Internet Cross Logo
Internet Cross your one stop web tutorial website

Listeners in Flash.

by Anand

Listeners are a new concept in flash. Now you can broadcast events to any number of objects with Listeners.

Suppose you wanted to make a text field respond to an event like a onMouseDown. This event is not a standard event that a textfield can react to. For the text field to react to this event you must register the textfield as a Listener for the event.

You have to use Mouse.addListener(textfieldname);
To remove a listener use Mouse.removeListener(textfieldname);

I will show you how you this to detect mousedown events.

1.Create a dynamic text field.
2.Give it an instance name of mult_txt set it to multiline.
3.In the first frame of the movie give action:

//add the listener to mult_txt
Mouse.addListener(mult_txt);

mult_txt.onMouseDown = function()
{
mult_txt.text += "mouse pressed at [ x:" + _root._xmouse+ " ] and [ y:" + _root._ymouse + " ]n";
mult_txt.selectable = false;
mult_txt.scroll += 1;

};

4. This Mouse.addListener(mult_txt); adds a listener to the mult_txt textfield
5. This mult_txt.onMouseDown = function(){} reacts to the event on mouse down on the screen and we display the x and y of the mouse pos in the text field.


[Download File][Page 1]
Visit our forums to discuss / post your problems.