Search code examples
actionscript-3flashnavigationanimate-cc

Actionscript navigation not working


In animate CC,(actionscript 3) I have 4 frames that need to go in sequence when the "A" or "D" buttons are pressed like so:

(((((1 -> 2 -> 3 -> 4|||and then back|||4 -> 3 -> 2 -> 1)))))

This works fine, but when i am back to frame 1 after the above sequence, clicking next sends me all the way to frame 4 (using trace I identified that all the event listeners triggered and I dont know why)

Below is the code for each frame:

Frame 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();

Frame 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();

Frame 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();

Frame 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();

I also tried using prevFrame(); and nextFrame(); but the same issue occurs leading me to believe my sequential logic is sound but my event listeners are the problem, please can someone shed some light on this for me.


Solution

  • You overcomplicated things. You don't need scripts in the each frame, just one common navigation script that rules it all.

    Option 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;
        }
    }
    

    Option 2.

    It's even better to put the frames into some MovieClip container, and to navigate it from the outside. Mixing frames and scripts, like you did, will bring you a lot of pain.