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

I'm struggling to do a simple Keypress to go to new scene?

Community Beginner ,
Apr 20, 2019 Apr 20, 2019

Copy link to clipboard

Copied

Can anyone help me with something I'm struggling to make happen. I'm sure it is pretty basic, but I'm just not skilled enough with the action codes

I have 6 scenes.

I want to use the standard keyboard number buttons (the ones at the top of the keyboard, not the number pad buttons on the side)

to navigate between the scenes.

So, key 1 goes to scene 1, key 2 goes to scene 2, and so on.

I want all of these to work in every scene, so I can move to any scene at any time.

(alternatively, if for some reason it's easier to use the Q,W,E,R,T,Y buttons instead, I'm happy to do that)

Any suggestions on writing this code?

Views

847

Translate

Translate

Report

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 , Apr 24, 2019 Apr 24, 2019

you can't force focus for a web application.  for an air app, you can.

Votes

Translate

Translate
Community Expert ,
Apr 21, 2019 Apr 21, 2019

Copy link to clipboard

Copied

import flash.events.KeyboardEvent;

stage.addEventListener(KeyboardEvent.KEY_DOWN,f);

function f(e:KeyboardEvent):void{

gotoAndStop(1,"Scene "+(e.charCode-48));

}

Votes

Translate

Translate

Report

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 Beginner ,
Apr 21, 2019 Apr 21, 2019

Copy link to clipboard

Copied

ok, I put that code in, but it didn't work.

Screen Shot 2019-04-21 at 14.23.11.png

I tried testing it and got these errors, and none of the buttons worked for changing between scenes...

Screen Shot 2019-04-21 at 14.23.39.png

Screen Shot 2019-04-21 at 14.23.53.png

Maybe, this is way too complicated for a beginner. I think I really don't understand this code stuff.

Votes

Translate

Translate

Report

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 ,
Apr 21, 2019 Apr 21, 2019

Copy link to clipboard

Copied

what are the names of your first few scenes?  if they're Scene 1, Scene 2 etc, use:

import flash.events.KeyboardEvent;

stage.addEventListener(KeyboardEvent.KEY_DOWN,f);

function f(e:KeyboardEvent):void{

gotoAndStop(1,"Scene "+(e.charCode-48));

}

Votes

Translate

Translate

Report

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 Beginner ,
Apr 21, 2019 Apr 21, 2019

Copy link to clipboard

Copied

Yes, Scene 1, Scene 2, etc...

So I should add the code like this:

import flash.events.KeyboardEvent;

stage.addEventListener(KeyboardEvent.KEY_DOWN,f);

function f(e:KeyboardEvent):void{

gotoAndStop(1,"Scene 1"+e.charCode);

}

import flash.events.KeyboardEvent;

stage.addEventListener(KeyboardEvent.KEY_DOWN,f);

function f(e:KeyboardEvent):void{

gotoAndStop(1,"Scene 2"+e.charCode);

}

And so on...

But, how does that make the "number key 1" go to "scene 1" and "number key 2" go to "scene 2"?? how do I tell it to what key goes to what scene?

Votes

Translate

Translate

Report

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 ,
Apr 22, 2019 Apr 22, 2019

Copy link to clipboard

Copied

no.

copy and paste the code below.  nothing needs to be added or changed.

e.charChode changes depending on the key used (but it needs to be adjusted)

import flash.events.KeyboardEvent;

stage.addEventListener(KeyboardEvent.KEY_DOWN,f);

function f(e:KeyboardEvent):void{

gotoAndStop(1,"Scene "+(e.charCode-48));

}

Votes

Translate

Translate

Report

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 Beginner ,
Apr 23, 2019 Apr 23, 2019

Copy link to clipboard

Copied

Ok so after much struggling and experimenting, I ended up using this bit of code to make it work...

stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_KeyboardDownHandler);

function fl_KeyboardDownHandler(event:KeyboardEvent):void

{

gotoAndStop(1,"Scene "+(event.charCode-48));

}

that's all. I don't even have to put the scene # in or change the...

e.charChode changes depending on the key used (but it needs to be adjusted)

...which I assumed by that you meant changing the number "48"?? to match the key code?

Anyways, I press 1 it goes to scene 1, pressing 2 goes to scene 2 and so on.

Now the only problem I have is making this work without having to first do a mouse click.

I tried adding...

stage.focus = this;

...before the above EventListener code, but it doesn't seem to make a difference. Would be really great if the keystrokes worked without requiring a mouse click first.

Any thoughts?

Votes

Translate

Translate

Report

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 ,
Apr 24, 2019 Apr 24, 2019

Copy link to clipboard

Copied

that's exactly the code i suggested.

Votes

Translate

Translate

Report

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 Beginner ,
Apr 24, 2019 Apr 24, 2019

Copy link to clipboard

Copied

Well, I'm not sure why your version didn't work??...your version and the version that ended up working are very very similar, yes, but not identical. Anyways, I'm super happy for your help, it definitely was critical to me making this work. Thanks.

any thoughts on my additional question?...

Now the only problem I have is making this work without having to first do a mouse click.

I tried adding...

stage.focus = this;

...before the above EventListener code, but it doesn't seem to make a difference. Would be really great if the keystrokes worked without requiring a mouse click first.

Any thoughts?

Votes

Translate

Translate

Report

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 ,
Apr 24, 2019 Apr 24, 2019

Copy link to clipboard

Copied

LATEST

you can't force focus for a web application.  for an air app, you can.

Votes

Translate

Translate

Report

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