Using event listeners

show more Using event listeners provides you with in-depth training on Web. Taught by Todd Perkins as part of the Flash CS3 Professional Beyond the Basics show less
please wait ...

Using event listeners

Now we'll talk about event listeners. Event listeners connect objects and events to functions that handle those events. If you're following along I'm working in 04_listeners.FLA in the Chapter 02 folder in the Exercise Files folder. This file has two layers, one called actions and one called button. The button layer contains an instance of a button called BTNbutton. In the instance name field in the Property inspector with the button selected I'm going to click and type an instance name for this button. I'm going to give this a simple instance name of my_btn.

Then I'm going to select the first keyframe in the actions layer and open the Actions panel using Option+F9 on the Mac or F9 on the PC. In the Actions panel notice the code that we wrote in last movie is here. This defines the function "button clicked" that receives a Mouse event, and simply traces 'button clicked.' So below the closed curly brace of this function, I'm going to connect the button on the stage to this function. Let's type my_btn.addEvent, with capital E, Listener, with a capital L, all one word and then in parentheses we need to pass in two values.

First the type of event. The type of event is a MouseEvent, with a capital M for mouse and a capital E for event. and then CLICK all caps. So a listener connects an object, in this case my_btn to an event, which is MouseEvent.CLICK. Events listeners not only connect objects with events, they connect an object and an event to an event handler function. To do that with we have to pass in a second parameter inside of the parentheses of this add event listener line of code.

So after MouseEvent.CLICK type a comma and a space and then type the name of the function that we want to run when the button gets clicked. The function name is buttonClicked. I'm getting that from line 1 in my code. Type buttonClicked, capital C. Make sure to close out the parentheses and type a semicolon to end the statement. So the line of code we just wrote adds an event listener to my_btn, that's the button on the stage, and the event the button is listening for or waiting for is a mouse click.

In Flash's language that's MouseEvent.CLICK. Once that button gets clicked Flash will run a function called buttonClicked, which we defined in the last movie on lines 1 through 4 in my code. So once the button is clicked we'll see button clicked in the Output window. Now let's test the movie to see this in action. The keyboard shortcut is Command+Return on the Mac, Ctrl+Enter on the PC. I'm going to click the button and watch the message in the Output window display button clicked. And there's a look at working with event listeners.

Using event listeners
Video duration: 2m 42s 5h 21m Intermediate


Using event listeners provides you with in-depth training on Web. Taught by Todd Perkins as part of the Flash CS3 Professional Beyond the Basics

Flash Professional
please wait ...