Skip to main content
Known Participant
September 6, 2023
Answered

Navigating with Arrow Keys - AS3

  • September 6, 2023
  • 1 reply
  • 548 views

Hello,

I am currently making a presentation where I need to use the left and right arrow keys to navigate movie clips. Everything seems to be working fine with the exception of the left arrow key. Here is my code.

 

This code is on frame 35 inside a movie clip. This movie clip is on Scene1, frame 3. The left key (37) will take the user back to Scene1, frame 2. The right key (39) will take the user to frame 36 on this movie clip.

stop();
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_KeyboardDownHandler);
function fl_KeyboardDownHandler(event:KeyboardEvent){
	if (event.keyCode == 37){
		MovieClip(parent).gotoAndStop(2);
	}
	else if (event.keyCode == 39){
		gotoAndPlay(36);
	}
}

This seems to work fine. However, when I have another action further down the timeline of this movie clip, the left key still wants to go to Scene 1, frame 2 and not to a frame within the movie clip. Here on frame 55 in the movie clip, the left key should take the user to frame 35 and the right key to frame 56. The right key works but the left key does not.

stop();
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_KeyboardDownHandler2);
function fl_KeyboardDownHandler2(event:KeyboardEvent){
	if (event.keyCode == 37){
		gotoAndStop(35);
	}
	else if (event.keyCode == 39){
		gotoAndPlay(56);
	}
}

 Any help would be greatly appreciated. Also, if you need more information please let me know.

 

Thanks,

Shannon

This topic has been closed for replies.
Correct answer kglad

yes, you need to do that:

 

stage.removeEventListener(KeyboardEvent.KEY_DOWN, fl_KeyboardDownHandler);

1 reply

kglad
Community Expert
Community Expert
September 6, 2023

did you remove the previous listener?

Known Participant
September 7, 2023

No, I didn't remove it. Is there code to remove a previously added listener before adding another? And again why would it work for the right arrow and not the left arrow?

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
September 7, 2023

yes, you need to do that:

 

stage.removeEventListener(KeyboardEvent.KEY_DOWN, fl_KeyboardDownHandler);