开发者

Actionscript 3.0 MovieClip frame change listener

开发者 https://www.devze.com 2023-04-12 20:52 出处:网络
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 mo

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.


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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号