Skip to main content
Participating Frequently
March 3, 2013
Answered

Help setting up back navigation button by label name.

  • March 3, 2013
  • 1 reply
  • 746 views

Newbie back for some gems of wisdom.

I am trying to get forward and back buttons to advance the user along the timeline by label name. I got the forward button to work, but can't seem to find the right code tweakage to get the back button to work.

Here is what I have:

stop();

 

var myLabels:Array = [ "A","B","C","D","E","F","G","H" ];

var nextLabel:String;

var inc:int = 1;

var prevLabel:String;

var inc2:int = -1;

 

fwdbtn.addEventListener(MouseEvent.CLICK, clickNextSection);

 

function clickNextSection(e:MouseEvent):void

{

    nextLabel = String(myLabels[inc]);

    gotoAndPlay(nextLabel);

    inc++;

}

 

bkbtn.addEventListener(MouseEvent.CLICK, clickPreviousSection);

 

function clickPreviousSection(e:MouseEvent):void

{

    prevLabel = String(myLabels[inc2]);

    gotoAndPlay(prevLabel);

    inc2++;

}

I'm sure there are some reduncies and I know I'm missing the variable that tells the timeline to subtract positioning from the array.

Do I need to define a new array for the back button?

Any idea what I'm messing up??

Thanks!!

This topic has been closed for replies.
Correct answer Ned Murphy

function clickNextSection(e:MouseEvent):void

{

     inc++;

    if(inc < myLabels.length){

        gotoAndPlay(String(myLabels[inc]));

    } else {

        inc = myLabels.length - 1; // keep it at the end   

    }

}

 

 

function clickPreviousSection(e:MouseEvent):void

{

    inc--;

    if(inc > -1 ){

         gotoAndPlay(String(myLabels[inc]));

    } else {

         inc = 0;  // keep it at the start

    }

}

1 reply

Ned Murphy
Legend
March 3, 2013

Just use the same inc variable instead of having two and don't increment or decrement it until you are about to use it.  That way it will always reflect where you are instead of where you might be next.

Participating Frequently
March 3, 2013

Thanks for the reply! Kind of lost unfortunately.

So can you tell me how to edit the code to reflect that? Not sure how what the deincriment syntax and is and not sure how to set it up the way you are suggesting - only adding deincriment/increment on the fly.

Any help you could lend to chopping the code would be appreciated.

Love the logic you suggest. That would ideal for it to work from any point.

Thanks for your help!

Ned Murphy
Ned MurphyCorrect answer
Legend
March 3, 2013

function clickNextSection(e:MouseEvent):void

{

     inc++;

    if(inc < myLabels.length){

        gotoAndPlay(String(myLabels[inc]));

    } else {

        inc = myLabels.length - 1; // keep it at the end   

    }

}

 

 

function clickPreviousSection(e:MouseEvent):void

{

    inc--;

    if(inc > -1 ){

         gotoAndPlay(String(myLabels[inc]));

    } else {

         inc = 0;  // keep it at the start

    }

}