Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
1

Navigating with Arrow Keys - AS3

Explorer ,
Sep 06, 2023 Sep 06, 2023

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

TOPICS
ActionScript , How to , Timeline
542
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Sep 07, 2023 Sep 07, 2023

yes, you need to do that:

 

stage.removeEventListener(KeyboardEvent.KEY_DOWN, fl_KeyboardDownHandler);
Translate
Community Expert ,
Sep 06, 2023 Sep 06, 2023

did you remove the previous listener?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 07, 2023 Sep 07, 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 07, 2023 Sep 07, 2023

yes, you need to do that:

 

stage.removeEventListener(KeyboardEvent.KEY_DOWN, fl_KeyboardDownHandler);
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 07, 2023 Sep 07, 2023

Thank you! That worked great! I didn't realize there was a remove function. I grately appreciate your time in answering this for me.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 07, 2023 Sep 07, 2023
LATEST

you're welcome.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines