Package | starling.utils |
Class | public class ButtonBehavior |
Inheritance | ButtonBehavior Object |
When reacting to touch input, taps can easily be recognized through standard touch
events via TouchPhase.ENDED
. However, you often want a more elaborate kind of
input handling, like that provide by Starling's Button class and its
TRIGGERED event. It allows users to cancel a tap by moving the finger away from
the object, for example; and it supports changing its appearance depending on its state.
Here is an example: a class that extends TextField and uses ButtonBehavior to add TRIGGER events and state-based coloring.
public class TextButton extends TextField { private var _behavior:ButtonBehavior; private var _tint:uint = 0xffaaff; public function TextButton(width:int, height:int, text:String="", format:TextFormat=null, options:TextOptions=null) { super(width, height, text, format, options); _behavior = new ButtonBehavior(this, onStateChange); } private function onStateChange(state:String):void { if (state == ButtonState.DOWN) format.color = _tint; else format.color = 0xffffff; } public override function hitTest(localPoint:Point):DisplayObject { return _behavior.hitTest(localPoint); } }
Instances of this class will now dispatch Event.TRIGGERED events (just like conventional buttons) and they will change their color when being touched.
Property | Defined By | ||
---|---|---|---|
abortDistance : Number The distance you can move away your finger before triggering is aborted. | ButtonBehavior | ||
enabled : Boolean Indicates if the button can be triggered. | ButtonBehavior | ||
minHitAreaSize : Number The target's hit area will be extended to have at least this width / height. | ButtonBehavior | ||
onStateChange : Function The callback that is executed whenever the state changes. | ButtonBehavior | ||
state : String The current state of the button. | ButtonBehavior | ||
target : DisplayObject [read-only] The target on which this behavior operates. | ButtonBehavior | ||
useHandCursor : Boolean Indicates if the mouse cursor should transform into a hand while it's over the button. | ButtonBehavior |
Method | Defined By | ||
---|---|---|---|
ButtonBehavior(target:DisplayObject, onStateChange:Function, minHitAreaSize:Number = 44, abortDistance:Number = 50) Create a new ButtonBehavior. | ButtonBehavior | ||
hitTest(localPoint:Point):DisplayObject Forward your target's hitTests to this method to make sure that the hit
area is extended to minHitAreaSize. | ButtonBehavior |
abortDistance | property |
abortDistance:Number
The distance you can move away your finger before triggering is aborted.
public function get abortDistance():Number
public function set abortDistance(value:Number):void
enabled | property |
enabled:Boolean
Indicates if the button can be triggered.
public function get enabled():Boolean
public function set enabled(value:Boolean):void
minHitAreaSize | property |
minHitAreaSize:Number
The target's hit area will be extended to have at least this width / height. Note that for this to work, you need to forward your hit tests to this class.
public function get minHitAreaSize():Number
public function set minHitAreaSize(value:Number):void
onStateChange | property |
onStateChange:Function
The callback that is executed whenever the state changes.
Format: function(state:String):void
public function get onStateChange():Function
public function set onStateChange(value:Function):void
state | property |
state:String
The current state of the button. The corresponding strings are found in the ButtonState class.
public function get state():String
public function set state(value:String):void
target | property |
target:DisplayObject
[read-only] The target on which this behavior operates.
public function get target():DisplayObject
useHandCursor | property |
useHandCursor:Boolean
Indicates if the mouse cursor should transform into a hand while it's over the button.
The default value is true
.
public function get useHandCursor():Boolean
public function set useHandCursor(value:Boolean):void
ButtonBehavior | () | Constructor |
public function ButtonBehavior(target:DisplayObject, onStateChange:Function, minHitAreaSize:Number = 44, abortDistance:Number = 50)
Create a new ButtonBehavior.
Parameterstarget:DisplayObject — The object on which to listen for touch events.
| |
onStateChange:Function — This callback will be executed whenever the button's state ought
to change. function(state:String):void
| |
minHitAreaSize:Number (default = 44 ) — If the display area of 'target' is smaller than a square of this
size, its hit area will be extended accordingly.
| |
abortDistance:Number (default = 50 ) — The distance you can move away your finger before triggering
is aborted.
|
hitTest | () | method |
public function hitTest(localPoint:Point):DisplayObject
Forward your target's hitTests
to this method to make sure that the hit
area is extended to minHitAreaSize
.
Parameters
localPoint:Point |
DisplayObject |