Tutorial Actionscript 3 – create link button, listeners, addEventListener, events handlers
This post is for those who are starting with actionscript 3 and still do not understand very well the use of listeners. Explained very simply the listener are sensors that can add to objects. These sensors warn us call a function whenever a certain event occurs.
In AS2 we were able to directly tell to the instances:
myObj.onClick() { }
and into this function write the behavior.
For Actionscript 3 you need to add a listener to the instance:
myObj.addEventListener(MouseEvent.CLICK, clickHandler); function clickHandler(event:MouseEvent) { trace("myObj pressed"); }
Thus we can capture a multitude of events that long objects. Here a small list of the most common:
Event.ADDED_TO_STAGE
Event.COMPLETE
Event.ENTER_FRAME
Event.INIT
Event.CHANGE
Event.ACTIVATE
Event.ADDED
Event.CLOSE
Event.RESIZE
Event.SELECT
MouseEvent.CLICK
MouseEvent.MOUSE_DOWN
MouseEvent.MOUSE_UP
MouseEvent.MOUSE_MOVE
MouseEvent.MOUSE_OVER
MouseEvent.MOUSE_OUT
MouseEvent.ROLL_OVER
MouseEvent.ROLL_OUT
NOTE: Not all the objects dispatch all events.
This would be an example of how to open a page. Create a LINK:
graphicInstance.addEventListener(MouseEvent.CLICK, mouseClick); function mouseClick(e:MouseEvent) { var url:String = "http://www.miguelmoraleda.com"; var request:URLRequest = new URLRequest(url); try { navigateToURL(request); } catch (e:Error) { // handle error here } }


I get a:
1120: Access of undefined property MouseEvent.
Am I not including some thing?
package {
import flash.display.*;
import flash.text.*;
public class HelloDan extends MovieClip {
vlost: You are missing the events package.
import flash.events.*
Actually, import flash.events.MouseEvent is enough
Happy new year!!
great thanks
Will you show an example where the listener calls a method within a class, instead of just a procedural function?
Hi Aaron,
To use listener within a class is the same as procedural functions. Here I will write a basic example
I hope this answer your question