<

Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x

AS3 Migration: Events

Published on
5,875 Points
2,775 Views
1 Endorsement
Last Modified:
This is a very simple example to help those of you who are still migrating from AS2 to AS3 understand the redesigned event model in AS3.

In AS2.0, event functions (that is, the function to be performed when an event is fired) were stored as a property of the function's caller. Hence, a "button clicked" function would be accomplished by

 
// AS2.0 code
myButton.onPress = function() {
	trace("button clicked")
};

Open in new window


in AS3.0, however, event functions are stored externally as independent functions, and only event listeners (something that checks to see if an event has happened) are attached to the caller itself.

Therefore, the above code snippet would translate as follows:

 
//AS3.0 code
import flash.events.MouseEvent;
// ...
myButton.addEventListener(MouseEvent.CLICK, clickFunction);

function clickFunction(e:MouseEvent) {
	trace("button clicked");
}

Open in new window


A few notes about this new event model

1: You need to import every type of event you will be calling. A few of the most common events include MouseEvent, TimerEvent, and just plain Event. If you want to play it safe, import flash.events.* (that will import all events).

2: The first parameter of your event function (clickFunction in this case) MUST be the type of event that the function is responding to. Also note that all events subclass Event, so the parameter (e:Event) will always work as long as you have imported Event.

3: Also, keep in mind that regardless of what event triggers the function, the function will ALWAYS be called from where it is defined in the code. That is, if you define a function on the main timeline and add a click listener to a button on the stage, trace(this) will return [object MainTimeline].
1
Comment
Author:Vega152
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
0 Comments

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Join & Write a Comment

In this tutorial viewers will learn how to create a basic motion tween animation in Flash Open a new document in Flash: Draw/import an image: Press CTRL + F8 to convert it into a graphic symbol: Select a frame (how long you want the tween to last): …
The goal of the tutorial is to teach the user how to select which audio input to use. Once you have an audio input plugged into the laptop or computer, you will go into the audio input settings and choose which audio input you want to use.
Next Article:

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month