1 / 16

Player 8.5 Event Model

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”

venus
Download Presentation

Player 8.5 Event Model

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Player 8.5 Event Model

  2. Player 8.5 Event Model • Based on W3C DOM http://www.w3.org/TR/DOM-Level-3-Events/events.html

  3. 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

  4. Bubbling Phase (3) Capture Phase (1) Target Phase (2)

  5. Event class: properties • bubbles • cancelable • target • currentTarget • eventPhase (CAPTURING_PHASE, AT_TARGET, BUBBLING_PHASE) • type (e.g. “mouseDown”)

  6. Event class: methods • preventDefault: void • isDefaultPrevented: Boolean • stopImmediatePropogation: void • stopPropagation: void • clone: Event

  7. 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”)); } }

  8. var pushButton:PushButton = new PushButton(); addChild(pushButton); pushButton.addEventListener(“push”, pushButton_push); ... private function pushButton_push(event:Event):void { trace(“button pushed”); }

  9. 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, ...); } }

  10. 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”); }

  11. Preventing default behaviour • cancelable • preventDefault() • isDefaultPrevented() • e.g. TextField (preventing char display)

  12. 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; } } }

  13. 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(); } }

  14. 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(); } }

  15. IEventDispatcher • addEventListener() • type:String (e.g. “push” or PushEvent.PUSH) • listener:Function (e.g. pushButton_push) • useCapture:Boolean = false • priority:int = 0

  16. 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”); } }

More Related