[Learn] AS2 to AS3 EnterFrame events
Posted: April 14th, 2008 | Author: John del Rosario | Filed under: AS 3.0, Flash, Learn | Tags: ActionScript, AS2, AS3, Flash, Learn, Tutorials | 4 Comments »NOTE: This blog post is inaccurate. I am sorry for the error. I have fixed the error following the comments posted.
So in this blog, I’ll be posting things I learn each day (hopefully), about programming, life, etc. The things I post here might not be completely correct or true, but they are what I learned and I post them as I understand them. Corrections would be very much appreciated.
I’ll call it the [Learn] series.
NOTICE: I am telling this as how I understand it from reading other tutorials on the web. This is possibly very inaccurate, but maybe you could understand things you haven’t understood before by reading how I understand it. :-/
Event handling in AS3.0 is very different from AS2.0. One of my most used events is onEnterFrame events.
In AS2.0, animating a simple box to move to the right every frame would look like this:
(please bear with me and the unformatted code)
box.onEnterFrame = function(){
this._x += 1; //execute this code everytime the box movieclip enters a frame.
}
but in AS3.0, it is a little longer, and I may still have to see the real benefits of this. But already, I am liking how easy it is to understand.
function moveBox(e:Event):void{
e.target.x += 1;
}
box.addEventListener(Event.ENTER_FRAME, moveBox);
the addEventListener method tells the box MovieClip to execute the function moveBox, everytime an ENTER_FRAME event is generated(which is all the time).
Notice the moveBox function has an Event parameter. When a function has an Event type parameter, it becomes a listener function. Passing a non-listener function to the addEventListener method causes an error. You could also add the moveBox listener to any other MovieClip you might have on stage, already one of the benefits of the new event handling of AS3.0.
There are a lot more events to discuss, but I hope this helps those who have just started converting to AS3 (like me). I’ll keep on posting more things I learn.
Hey, sorry but is this code a little wrong? Cuz when I try it out, ‘this’ refers to the MainTimeline, not the object… :S
Hi Noah, which version are you talking about, AS2 or AS3?
Sorry to say this, but you have actually Missed the whole point of the new addEventListener in AS3
…
function moveBox(e:Event):void {
this.x += 1; // WRONG
}
box.addEventListener(Event.ENTER_FRAME, moveBox);
you are not Moving the Box, You are moving the Timeline/Class Object
in as3, ‘this’ ALWAYS refers to the Timeline/Class and does not change depending on the object calling the event.
to reference the object you use “e.target” [or "e.currentTarget"] // there are differences which I wont go into.
Yeah, sorry about that. Like I’ve said on the post, it was what I understood when starting out learning AS3.. I got confused.
I just forgot about this post over the months.. Thanks for clearing this up WORMSS.
Again, I apologize for the confusion.
I’ll edit this post later when I get home. Cheers!