Presentation is loading. Please wait.

Presentation is loading. Please wait.

AS3: Events & Listeners eventListeners in ActionScript 3.

Similar presentations

Presentation on theme: "AS3: Events & Listeners eventListeners in ActionScript 3."— Presentation transcript:

1 AS3: Events & Listeners eventListeners in ActionScript 3

2 What are events & listeners? An event is something that happens while your movie runs, like:  key press, mouse click, mouse movement, new frame, timer tick, loading an asset… Some events are initiated by the user. Some events happen as part of the program running. They can be used to make your program interactive.

3 Registering an event listener To respond to an event we must:  Choose which object is going to respond or ‘listen’ to the event and attach a listener to it.  Add the event listener  Indicate which event is being listened to  Give the name of a function that will be called when the event occurs. objName.addEventListener( ); EventName, eventListenerFunction This is a blank text box because it’s the only way I know of hiding the text underneath it. Surely there must be another way.

4 Registering an event listener: example To create a listener for a click event, attached to a button called ‘btnStart’ that will call a function called ‘startGame’ btnStart.addEventListener(MouseEvent.CLICK, startGame); Function startGame( evt:MouseEvent ) :void { trace(, “ was clicked”); trace(btnStart.label, “ was clicked”); } Using is more flexible than using the name of the button.

5 Event listener: example 2 stage.addEventListener(KeyboardEvent.KEY_DOWN, moveShip); To listen for a key press that will call a function called ‘moveShip’ we might attach the event to the stage Function moveShip( evt:KeyboardEvent ) :void { trace(evt.keyCode, “ was clicked”); } remember if you’re needing to check what the keyCode is you should not check the keyCode number (if (keyCode == 40)) but use the constants, like (if (keyCode == Keyboard.DOWN)) instead. You’ll need to import flash.ui.Keyboard to have access to them

6 Listeners on container affecting children Say you have multiple animated objects and when you click on one you want to make it disappear. Rather than add a listener to each ‘child’ add one to the parent, like this: var container:Sprite = new Sprite(); container.addChild(obj1); container.addChild(obj2); etc… stage.addChild(container); container.addEventListener(MouseEvent.CLICK, hideObject); private function hideObject( evt: MouseEvent ):void { = false; }

7 Making the children reappear Perhaps we can make a startButton make the children visible again var btnStart:Button = new Button(); btnStart.label = “Start”; stage.addChild(btnStart); btnStart.addEventListener(MouseEvent.CLICK, startGame); private function startGame( evt: MouseEvent ):void { var countChld:uint = this.container.numChildren; for (var nChild:uint=0; nChild < countChld; nChild++) { this.container.getChildAt(nChild).visible = true; } }

8 Removing listeners If you add a listener to an object that you later destroy you MUST remove any listeners BEFORE destroying the object. private function killObject( evt: MouseEvent ):void {, killObject); = null; }

Download ppt "AS3: Events & Listeners eventListeners in ActionScript 3."

Similar presentations

Ads by Google