如何正确使用currentFrame/totalFrames AS3

How to use currentFrame/totalFrames Properly AS3

嘿,大家已经研究了一段时间了,但似乎无法弄清楚。

基本上我想做的是当我的 introScreen 完成动画时我想删除 introScreen 并添加 startScreen 因为这两个 Movie Clip 对象包含动画,我不希望一个开始而另一个还在继续。所以我试图使用 Frame 属性来检查这个条件并执行它。

这是我目前所拥有的,但它除了播放 introScreen 之外什么都不做,只是停留在那个屏幕上。

//Start Game Screen
        introScreen = new mcIntroScreen();
        stage.addChild(introScreen);
        introScreen.x = (stage.stageWidth / 2);
        introScreen.y = (stage.stageHeight / 2);

        if (introScreen.currentFrame == introScreen.totalFrames)
        {
            stage.removeChild(introScreen);

            //Start Game Screen
            startScreen = new mcStartGameScreen();
            stage.addChild(startScreen);
            startScreen.x = (stage.stageWidth / 2);
            startScreen.y = (stage.stageHeight / 2);
            trace(startScreen + "startscreenADDED");
            startScreen.addEventListener("PLAY_AGAIN", playGameAgain, false, 0, true);

            startScreen.addEventListener("TIME_ATTACK", timeAttackMode, false, 0, true);
        }

也许有人可以帮我解决这个问题我认为当前的代码意味着如果当前帧播放并达到动画中的总帧数那么条件就会满足我也尝试跟踪它但没有任何结果向上。任何帮助,将不胜感激。

您需要将帧检查放在 Event.ENTER_FRAME 处理程序中。就像现在一样,除非您遗漏了一些代码,否则它只会在创建 introScreen 后立即检查一次,这当然意味着它仍在第 1 帧上。尝试这样的事情:

// Start Game Screen
introScreen = new mcIntroScreen();
// etc

addEventListener(Event.ENTER_FRAME, introEnterFrameHandler);

function introEnterFrameHandler(e:Event):void {
    if (introScreen.currentFrame == introScreen.totalFrames)
    {
        removeEventListener(Event.ENTER_FRAME, introEnterFrameHandler);
        stage.removeChild(introScreen);
        // etc
    }
}

请注意,这意味着不会真正看到最后一帧。您可以使用 Event.EXIT_FRAME 让最后一帧在处理结尾之前显示。

此外,我通常这样做的方式有点不同。我没有使用 Event.ENTER_FRAME 跟踪回放,而是在时间轴末尾发送了一个事件。示例:

// on the last frame of the symbol timeline
dispatchEvent(new Event("introComplete", true));

那我就可以简单的把它当作一个自定义事件来处理了:

// Start Game Screen
introScreen = new mcIntroScreen();
// etc
addEventListener("introComplete", introCompleteHandler);
function introCompleteHandler(e:Event):void {
    stage.removeChild(introScreen);
    // etc
}