Nick
Nick

Reputation: 3451

Actionscript 3.0 MovieClip frame change listener

I have a scene with a monster. Monster blinks its eyes. Eyes blinking is a separate MovieClip. Under some conditions, monster opens its mouth. Mouth opening is a separate MovieClip as well. At this moment I have to hide eyes MovieClip. As soon as monster closes its mouth, I must make eyes blinking visible again.

My idea is to have some listener that listens to Mouth_opening MovieClip. When Mouth opening's currentFrame changes from 1 to 2, I will hide eyes. When it changes from 2 to 1, I will show it back.

So, the question is: how do I listen to currentFrame changes? Event.ENTER_FRAME is not appropriate: it's being called every render frame, not only when currentFrame changes.

Upvotes: 4

Views: 3196

Answers (1)

laurent
laurent

Reputation: 90843

There is no "frameChange" event in ActionScript so you need to come up with your own system. For example, you can make it work by listening to enterFrame then monitor the current frame - when it changes call your function. Something like that should work:

private var lastFrame:int = -1;

// Then add this in your constructor:
addEventListener("enterFrame", onEnterFrame);

private function onEnterFrame(event:*):void {
    if (lastFrame != currentFrame) {
        onFrameChanged();
        lastFrame = currentFrame;
    }
}


private function onFrameChanged():void {
    trace("The frame has changed to " + currentFrame);
}

Upvotes: 5

Related Questions