How to Create an Event Listener in Adobe Flash CS4
Adobe Flash CS4 has come a long way from its early days as a simple animation tool. Now combined with the programming language ActionScript 3.0 you can do some pretty amazing things. In this tutorial I am going to show you how to create an event listener. An event is something that occurs such as the user clicking the mouse and the listener is the function that will execute when that event takes place. Here is how you create an event listener in Adobe Flash CS4 with ActionScript 3.0.
- Launch Adobe Flash CS4 and select Create New Flash File (Action Script 3.0).
-
Step 2
Rename your layer “actions” by double clicking the layer name in your layers panel.
-
Step 3
Now we start creating our code. Click on frame 1 on the actions layer, then select “Window” from the main menu at the top, and then select “Actions”. This will bring up the action script window.
-
Step 4
Type in the code as it is shown in the picture associated with this step. The first line reads stop();. That tells flash to not loop our program when we run it.
The second line:
stage.addEventListener(MouseEvent.CLICK,buttonClickListener);registers a mouse click event called “buttonClickListener” to the stage. So everytime we click the mouse on the stage the event buttonClickListener will execute.
-
Step 5
Now we write the code of our event listener in this case “buttonClickeListener”. This function says that when the user clicks the mouse we will print the words “button clicked” in our output window. To see the code you have to look at the associated picture with this step.
-
Step 6
In the function the parameter “e” can be named anything you want but it must be of type “MouseEvent”. We set our return value for our function to “void” since we do not need or expect a return value.
Now run the program by clicking the “CTRL+ENTER” keys on your keyboard, and then start clickin on the stage.