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?
you can't force focus for a web application. for an air app, you can.
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));
}
Copy link to clipboard
Copied
ok, I put that code in, but it didn't work.
I tried testing it and got these errors, and none of the buttons worked for changing between scenes...
Maybe, this is way too complicated for a beginner. I think I really don't understand this code stuff.
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));
}
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?
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));
}
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?
Copy link to clipboard
Copied
that's exactly the code i suggested.
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?
Copy link to clipboard
Copied
you can't force focus for a web application. for an air app, you can.