Skip to main content
Participating Frequently
March 31, 2013
Answered

Help setting up arrow key navigation from label to label.

  • March 31, 2013
  • 1 reply
  • 663 views

I tried editing some code I was able to use to add button click navigation from label to label.

My goal was to setup backwards and forward navigation using the keyboard.

I've clearly messed something up big.

Can anyone help me fix this code???

import flash.events.KeyboardEvent;

import flash.ui.Keyboard;

stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyboardHandler);

var myLabels:Array = ["intro","study_obj","exec_sum","country","submenu","us","china","germany"];

var nextLabel:String;

var inc:int = -1;

var PrevLabel:String;

stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyboardHandler);

function KeyboardHandler(event:KeyboardEvent):void

{

  if(event.keyCode == 37)

{

     inc++;

    if(inc < myLabels.length){

        gotoAndPlay(String(myLabels[inc]));

    } else {

        inc = 0;  // keep it at the start  

    }

}

function KeyboardHandler(event:KeyboardEvent):void

{

  if(event.keyCode == 39)

{

{

    inc--;

    if(inc > -1 ){

         gotoAndPlay(String(myLabels[inc]));

    } else {

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

    }

}

Sorry. I'm a newbie and having trouble.

Thank you!

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

You cannot have two functions with the same name.  So combine the code for both into one and see if that gets you any closer....

function KeyboardHandler(event:KeyboardEvent):void

{

    if(event.keyCode == 37) {

        inc++;

        if(inc < myLabels.length){

             gotoAndPlay(String(myLabels[inc]));

        } else {

             inc = 0;  // keep it at the start  

        }

    } else if(event.keyCode == 39) {

        inc--;

        if(inc > -1 ){

             gotoAndPlay(String(myLabels[inc]));

        } else {

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

        }

    }

}

1 reply

Ned Murphy
Ned MurphyCorrect answer
Legend
March 31, 2013

You cannot have two functions with the same name.  So combine the code for both into one and see if that gets you any closer....

function KeyboardHandler(event:KeyboardEvent):void

{

    if(event.keyCode == 37) {

        inc++;

        if(inc < myLabels.length){

             gotoAndPlay(String(myLabels[inc]));

        } else {

             inc = 0;  // keep it at the start  

        }

    } else if(event.keyCode == 39) {

        inc--;

        if(inc > -1 ){

             gotoAndPlay(String(myLabels[inc]));

        } else {

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

        }

    }

}

Participating Frequently
March 31, 2013

AWESOME!!! Works great! Thank you!!

Ned Murphy
Legend
April 4, 2013

You're welcome