160 likes | 246 Views
Player 8.5 Event Model. Player 8.5 Event Model. Based on W3C DOM http://www.w3.org/TR/DOM-Level-3-Events/events.html. MXML :. <Application> <Panel mouseDown=“trace(‘panel: mouseDown’)”> <Tile mouseDown=“trace(‘tile: mouseDown’)”> <Label text=“Touch Me”
E N D
Player 8.5 Event Model • Based on W3C DOM http://www.w3.org/TR/DOM-Level-3-Events/events.html
MXML: <Application> <Panel mouseDown=“trace(‘panel: mouseDown’)”> <Tile mouseDown=“trace(‘tile: mouseDown’)”> <Label text=“Touch Me” mouseDown=“trace(‘label: mouseDown’)” /> </Tile> </Panel> </Application> Output: label: mouseDown tile: mouseDown panel: mouseDown
Bubbling Phase (3) Capture Phase (1) Target Phase (2)
Event class: properties • bubbles • cancelable • target • currentTarget • eventPhase (CAPTURING_PHASE, AT_TARGET, BUBBLING_PHASE) • type (e.g. “mouseDown”)
Event class: methods • preventDefault: void • isDefaultPrevented: Boolean • stopImmediatePropogation: void • stopPropagation: void • clone: Event
Example class PushButton extends Sprite { public function PushButton() { addEventListener(MouseEvent.MOUSE_DOWN, this_mouseDown); } private function this_mouseDown(event:Event):void { dispatchEvent(new Event(“push”)); } }
var pushButton:PushButton = new PushButton(); addChild(pushButton); pushButton.addEventListener(“push”, pushButton_push); ... private function pushButton_push(event:Event):void { trace(“button pushed”); }
Custom event class: PushEvent public class PushEvent extends Event { public static const PUSH:String = “push”; public function PushEvent(type:String, bubbles:Boolean = false, cancelable:Boolean = false, ...) { super(type, bubbles, cancelable, ...); } }
Usage: // dispatching... dispatchEvent(new PushEvent(PushEvent.PUSH)); .... // listening... pushButton.addEventListener(PushEvent.PUSH, pushButton_push); ... // handling... private function pushButton_push(event:PushEvent):void { trace(“button pushed”); }
Preventing default behaviour • cancelable • preventDefault() • isDefaultPrevented() • e.g. TextField (preventing char display)
class PushButton extends Sprite { public function PushButton() { addEventListener(MouseEvent.MOUSE_DOWN, this_mouseDown); addEventListener(PushEvent.PUSH, this_push, false, -10); } private function this_mouseDown(event:Event):void { dispatchEvent(new Event(“push”)); } private function this_push(event:PushEvent):void { if (!event.isDefaultPrevented()) // guard { alpha = 0.5; } } }
var pushButton:PushButton = new PushButton(); addChild(pushButton); pushButton.addEventListener(“push”, pushButton_push); ... private function pushButton_push(event:Event):void { if (someCondition()) { event.preventDefault(); // prevent default behaviour performCustomBehaviour(); } }
Stopping propagation • stopPropagation() • stopImmediatePropagation() // TextInput.as override public function keyDownHandler(event:KeyboardEvent):void { if (event.keyCode == Keyboard.ENTER) { dispatchEvent(new Event("enter")); } else { // Swallow all keyDown events. event.stopPropagation(); } }
IEventDispatcher • addEventListener() • type:String (e.g. “push” or PushEvent.PUSH) • listener:Function (e.g. pushButton_push) • useCapture:Boolean = false • priority:int = 0
Using IEventDispatcher class PushButton extends SomeOtherClass implements IEventDispatcher { private var eventDispatcher:EventDispatcher; public function PushButton() { eventDispatcher = new EventDispatcher(this); } private function dispatchPushEvent():void { eventDispatcher.dispatchEvent(“push”); } }