Event Listeners
==========================================
=========================
Listeners in Flash make user interaction much easier to work with. You can intercept Key presses, mouse movements and clicks no matter which component is focused at the time. The 2 most interesting things about listeners is that they allow you to know when a key is released, or when the user uses the scroll wheel on his mouse!
The Basics
A listener is basically an object. To create a listener object, there are a few things you have to do:
-First, create a new object.
var ListenerObject:Object = new Object()
-Second, make it actually listen to things. For that we use the addListener(object) function that comes in the Key and Mouse classes:
//For a mouse listener
Mouse.addListener(ListenerObject);
//Or for a Key listener
Key.addListener(ListenerObject);
-Third, create the functions for the events that get triggered:
ListenerObject.onSomething = function()
{
//do something
};
Key Listeners
Key listeners allow you to know when a key is pressed, or when a key is released. The two event names are obviously onKeyDown and onKeyUp. In an onKeyXX event, youcan access the Key object, which stores the actual key pressed or released.
There are a few functions in the Key object, but the 2 main ones we need are :
-getCode() : A key code is a number used to store the key currently pressed or release. It is a special Flash-assigned number that can also keep track of when the space key is pressed, for example.
-getAscii(): This is to retrieve the ASCII value of the key. An ASCII value is also a number, but it is used to store what actual character the Key is. List of ASCII characters. This value can be used with the String function fromCharCode(Number).
Here is an example that traces what key is actually pressed, and also traces what key is released:
var ListenerObject:Object = new Object();
ListenerObject.onKeyDown = function()
{
trace("You pressed the "+String.fromCharCode(Key.getAscii())+" key");
}
ListenerObject.onKeyUp = function()
{
trace("You released the "+String.fromCharCode(Key.getAscii())+" key");
}
Key.addListener(ListenerObject);
Note: If a user holds down a key, the onKeyDown event will be triggered as long as the key is pressed. The frequency of the repetition all depends on the user's settings.
Mouse Listeners
Mouse Listeners allow you to know when the user clicks, releases or moves the mouse button, or when he uses the scroll wheel. The events are onMouseDown,onMouseUp,onMouseMove and onMouseWheel.
You've probably already used the first 3 in your games, etc. So I'm, just going to explain how to use onMouseWheel:
If you want to know by how much the mouse wheel has moved, you need to add a parameter to your function: a number. This parameter represents by how much the scroll wheel has moved. It's a positive number if the user scroll up, and a negative number if the user scrolls down.
Examples are worth a thousand words, so here's how you can make a movieclip move up or down using only the scrollwheel. Hopefully you actually have a scroll wheel on your mouse.
var ListenerObject:Object = new Object();
ListenerObject.onMouseWheel = function(inc:Number)
{
_parent.movingMC._y -= inc;
}
Mouse.addListener(ListenerObject);
Note: It is likely that a user won't have a mouse wheel. You should always add a key alternative to the mouse wheel!
This concludes this tutorial about Listeners. Hopefully it was clear enough, and I've made a small example that basically listens for every user interaction. Click here to view it.