How to Create an Event Listener in Flash CS4
Adobe's Flash platform allows developers to create a wide variety of interactive animations, applications and games. Flash uses a scripting language known as ActionScript to create the interactive elements of Flash animations and apps. One important part of ActionScript in Adobe CS4 is the ability to create event listeners. Event listeners are small pieces of code that monitor buttons or other parts of the Flash movie they are part of, creating additional actions when interaction from the user is detected.
- Difficulty:
- Moderately Easy
Instructions
-
-
1
Launch the Flash CS4 program. On the initial Flash splash screen, select the option to create a new ActionScript 3.0 project.
-
2
Add an object to the stage that you intend to use as a button or other interactive element. You can create the object directly in Flash or you can import it from another program using the "Import" function in the File menu.
-
3
Click the object to select it. Select the "Convert to Symbol" option from the Modify menu, giving the object a name and setting its symbol type to "Button." Click "OK" to perform the conversion. Once converted, view the properties of the button on the right side of the screen and give it an instance name as well.
-
4
Click the first frame of the timeline for your project to select it. Press the "F9" key to open the ActionScript scripting window for the frame.
-
5
Create an event that will run when the event listener detects a click from a user. The event can be any scripted event function that accomplishes what you want your Flash animation to do when the button is clicked; the event could play a different movie clip, pop up a message to the user or perform other actions.
-
6
Add the line "instance_name.addEventListener(MouseEvent.CLICK, eventFunction);" without the quotes, making sure that the punctuation and capitalization are correct. Substitute the instance name of the button you created for "instance_name" and the name of the event function that you want to run when the button is clicked for "eventFunction".
-
7
Save your project and use the "Test Movie" option in the Control menu to confirm that your event listener works correctly.
-
1
Tips & Warnings
Event listeners can monitor a number of other interactions in addition to mouse clicks, even events that do not involve buttons. Other common event listeners include "MouseEvent.DOUBLE_CLICK", "MouseEvent.MOUSE_OVER" and "VideoEvent.COMPLETE".