Actionscript 导航不工作

Actionscript navigation not working

在 animate CC 中,(actionscript 3) 当按下 "A" 或 "D" 按钮时,我有 4 个帧需要按顺序进行:

((((1 -> 2 -> 3 -> 4|||然后返回|||4 -> 3 -> 2 -> 1)))))

这工作正常,但是当我在上述序列之后回到第 1 帧时,单击下一步将我一直发送到第 4 帧(使用跟踪我确定所有事件侦听器都已触发但我不知道为什么)

下面是每一帧的代码:

第 1 帧

stage.addEventListener(KeyboardEvent.KEY_DOWN, nextFram);
function nextFram(e:KeyboardEvent):void{
if (currentLabel == "home" && e.keyCode == Keyboard.D){
 trace("gotobuttonpage");
 gotoAndPlay("buttonPage");
 removeEventListener(KeyboardEvent.KEY_DOWN, nextFram);
 trace("event listener removed");
 
} 
}
stop();

第 2 帧

stage.addEventListener(KeyboardEvent.KEY_DOWN, nextFram2);
function nextFram2(e:KeyboardEvent):void{
if (currentLabel == "buttonPage" && e.keyCode == Keyboard.D){
 trace("gotovideopage");
 gotoAndPlay("videoPage");
 removeEventListener(KeyboardEvent.KEY_DOWN, nextFram2);
 trace("event listener removed");
 
} 
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, backFram2);
function backFram2(e:KeyboardEvent):void{
if (currentLabel == "buttonPage" && e.keyCode == Keyboard.A){
 trace("backtohomepage");
 gotoAndPlay("home");
 removeEventListener(KeyboardEvent.KEY_DOWN, backFram2);
 trace("event listener removed");
 
} 
}
stop();

第 3 帧

stage.addEventListener(KeyboardEvent.KEY_DOWN, nextFram3);
function nextFram3(e:KeyboardEvent):void{
if (currentLabel == "videoPage" && e.keyCode == Keyboard.D){
 trace("gotoendpage");
 gotoAndPlay("endPage");
 removeEventListener(KeyboardEvent.KEY_DOWN, nextFram3);
 trace("event listener removed");
} 
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, backFram3);
function backFram3(e:KeyboardEvent):void{
if (currentLabel == "videoPage" && e.keyCode == Keyboard.A){
 trace("gotobuttonpage!");
 gotoAndPlay("buttonPage");
 removeEventListener(KeyboardEvent.KEY_DOWN, backFram3);
 trace("event listener removed");
} 
}
stop();

第 4 帧

stage.addEventListener(KeyboardEvent.KEY_DOWN, backFram4);
function backFram4(e:KeyboardEvent):void{
if (currentLabel == "endPage" && e.keyCode == Keyboard.A){
 trace("backtovideopage");
 gotoAndPlay("videoPage");
 removeEventListener(KeyboardEvent.KEY_DOWN, backFram4);
 trace("event listener removed");
 
} 
}
stop();

我也试过使用prevFrame();和下一个框架();但同样的问题发生让我相信我的顺序逻辑是合理的但我的事件监听器是问题所在,请有人能为我阐明这一点。

你把事情想得太复杂了。您不需要每一帧中的脚本,只需要一个通用的导航脚本即可。

选项 1.

// Frame 1 script.

var Frames:Array;
var currentPage:int;

// Initialize for the first time.
if (!Frames)
{
    stop();

    // Subscribe for the keyboard event just this once.
    stage.addEventListener(KeyboardEvent.KEY_DOWN, onKey);
    Frames = ["home", "buttonPage", "videoPage","endPage"];

    // Go to the first page, if necessary.
    naviGate(-100);
}

function naviGate(value:int):void
{
    // Shift the current page.
    currentPage += value;

    // Fix the ranges.
    if (currentPage < 0)
    {
        currentPage = 0;
    }

    if (currentPage >= Flames.length)
    {
        currentPage = Frames.length - 1;
    }

    // Go to designated frame.
    gotoAndStop(Frames[currentPage]);
}

// Now, simple keypress handling.
function onKey(e:KeyboardEvent):void
{
    switch (e.keyCode)
    {
        case Keyboard.A:
            naviGate(-1);
            break;

        case Keyboard.D:
            naviGate(1);
            break;
    }
}

选项 2.

最好将帧放入某些 MovieClip 容器中,并从外部进行导航。像你这样混合框架和脚本,会给你带来很多痛苦。