Home > Actionscript 3, Flash > Tutorial Actionscript 3 – create link button, listeners, addEventListener, events handlers

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
    }
  }



Categories: Actionscript 3, Flash Tags:
  1. vlost
    December 31st, 2009 at 16:23 | #1

    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 {

  2. December 31st, 2009 at 23:14 | #2

    vlost: You are missing the events package.

    import flash.events.*

    Actually, import flash.events.MouseEvent is enough :)

    Happy new year!!

  3. uyanga
    January 25th, 2010 at 00:55 | #3

    great thanks

  4. February 8th, 2010 at 10:14 | #4

    Will you show an example where the listener calls a method within a class, instead of just a procedural function?

  5. February 8th, 2010 at 10:25 | #5

    Hi Aaron,

    To use listener within a class is the same as procedural functions. Here I will write a basic example

    package
    {
        import flash.display.*;
        import flash.events.*;
     
        public class ExampleListener extends Sprite
        {
            private var _clip:Sprite;
     
            public function ExampleListener()
            {
                super();
                createClip();
                _clip.addEventListener(MouseEvent.CLICK, clipClicked);
            }
            private function clipClicked(event:MouseEvent)
            {
                trace("Clip Clicked");
            }
            private function createClip()
            {
                //create some draw in the clip and add it to the stage
            }
        }
    }

    I hope this answer your question :)

Spam Protection by WP-SpamFree

Get Adobe Flash playerPlugin by wpburn.com wordpress themes