Creating a Project Using Listeners


In this project, we will create a simple History application that will output each time the user clicks the mouse or presses a key on the keyboard. This example will demonstrate how to use listeners in a project.

1.

Open MouseKeyboardHistory1.fla in the Lesson07/Start folder.

This FLA file has two layers: Actions and Output. The Actions layer is where we'll place the actions for this project. The Output layer contains a TextField object with an instance name of output.

2.

With the Actions panel open, select Frame 1 of the Actions layer and add the script:

   var history:Object = new Object();


The first step is to create an empty Object called history, which will act as our listener target. We will then add our specific listener methods to this object.

3.

Add the following event handler after the script you added in Step 2:

   Key.addListener(history);


To register our history Object with the Key class, we simply need to call the addListener method. Now, whenever a Key event happens, our history object will be notified. So let's go ahead and handle one of those events.

4.

Add the following event handler after the script you added in Step 3:

   history.onKeyDown = function() {      var action:String = "[KEY] " + chr(Key.getAscii());      output.text = action + newline + output.text;    }


The onKeyDown method is called on all registered listener objects whenever one of the keys on the keyboard it pressed. In our example, we'll create a string to add to the output TextField. To determine which key was pressed, we can use either the static getAscii or getCode methods of the Key class. For our example, we want a string representing that key, so we'll use the getAscii method and convert that to a string using the built-in chr method.

5.

Add the following event handler after the script you added in Step 4:

   Mouse.addListener(history);


We will also register our history Object with the Mouse class. This syntax is the same as what was used to register with the Key class and is consistent with all the built-in classes in Flash that use listeners.

6.

Add the following event handler after the script you added in Step 5:

   history.onMouseDown = function() {      var action:String = "[MOUSE] " + _level0._xmouse + " x " + ¨        _level0._ymouse;      output.text = action + newline + output.text;    }


Now that our history Object is also listening for Mouse events, we'll create a method to be called when the mouse is pressed. This event is called onMouseDown and has the same goal as our Key listener. We want to output a string that represents that click, so we'll output the x and y coordinates of the mouse position at the time of the click using the _xmouse and _ymouse properties of _level0.

7.

From the menu, choose Control > Test Movie to see the movie in action.

Verify that your code is working by clicking the mouse anywhere on the stage and pressing random keys on the keyboard. Notice the history text appearing in the TextField.

8.

Close the test movie to return to the authoring environment. Save your project as MouseKeyboardHistory2.fla.

This completes the exercise. You should now have a basic understanding of listeners and the ways you can use them in your Flash projects.




Macromedia Flash 8 ActionScript Training from the Source
Macromedia Flash 8 ActionScript: Training from the Source
ISBN: 0321336194
EAN: 2147483647
Year: 2007
Pages: 221

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net