Frame labels in timeline animation using as3

I didn’t have many oportunities to go wild (yeah I said it!) with actionscript 3 lately. Therefore when the other day I was working on rather simple thing, I got stuck, to say the least. But let’s face it, even actionscript superstars (i’m not talking about myself, duh!) sometimes forget about simplest things, for that occasion is this post.

 So let’s cut the chit-chat and go straight to it. I have movie clip named mcmain on my main scene, which have a timeline animation in it, which ends on the 30th frame. I want to call some other action when the animation stops. Here is the code that will do it. (remember to import classes)

import flash.events.Event;
//adds event listener to MovieClip with animation
mcmain.addEventListener(Event.ENTER_FRAME, currFrameCheck);
private function currFrameCheck(event:Event):void {
    if(mcmain.currentFrame == 30){
       //do something // remember to remove event listener when it's not needed anymore  
       mcmain.removeEventListener(Event.ENTER_FRAME, currFrameCheck); 
    }else{ 
       trace(mcmain.currentFrame); 
    } 
}

Piece of cake, right? :-) But while we are coding we can do it right, (right?:-)) instead putting instance name in function, I can do the function reusable. All I need to do is replace instance name with event.target, and I’m good to go, here is what it looks like:

import flash.events.Event; 
//adds event listener to MovieClip with animation 
mcmain.addEventListener(Event.ENTER_FRAME, currFrameCheck); 
private function currFrameCheck(event:Event):void {  
    if(event.target.currentFrame == 30){ 
        //do something // remember to remove event listener when it's not needed anymore 
       event.target.removeEventListener(Event.ENTER_FRAME, currFrameCheck); 
    }else{
       trace(event.target.currentFrame); 
    } 
}

Doable? Yes indeed! Allright, everything looks fine but what if I change my animation and the end wont be on 30th frame? Or I don’t want to all my animations to end on the exact same frame? Changing frame number to frame label will do the trick, in as3 it’s easy, just replace currentFrame with currentLabel and it’s done. But if my (briliant of course) description isn’t enough, here is what the code looks like:

import flash.events.Event; 
//adds event listener to MovieClip with animation 
mcmain.addEventListener(Event.ENTER_FRAME, currFrameCheck); 
private function currFrameCheck(event:Event):void {  
    if(event.target.currentLabel == ”endframe”){ 
        //do something // remember to remove event listener when it's not needed anymore 
        event.target.removeEventListener(Event.ENTER_FRAME, currFrameCheck); 
    }else{ 
        trace(event.target.currentLabel); 
    } 
}

Ok, looks like that’s all for today, thanks for reading this post, I hope that this information was useful. Hapy New Year for all you guys:-)

 

0 0 vote
Article Rating