Copy link to clipboard
Copied
I want the user of my game to be able to press the Space bar and be taken to a new scene. I know how to do this with a button, but I can't figure out the code to use for a Keyboard event.
Can anyone help me to figure out how to make pressing the spacebar play a new scene?
If the intention is to actually stop at those frames, then use gotoAndStop() instead of gotoAndPlay(). If you tell something to gotoAndPlay where it already is, any stop() command you might have had in that frame is already used up, so it plays on.
Copy link to clipboard
Copied
Ok, I was able to figure this out by using this code:
stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyPressed);
function KeyPressed(evt:KeyboardEvent):void {
Keyboard.SPACE
gotoAndPlay(8);
}
But for some reason the screen has to be clicked before the space bar works.
Copy link to clipboard
Copied
Your function code will react to any key the way you wrote it, not just the SPACE. To only have it work for the SPACE you should use..
function KeyPressed(evt:KeyboardEvent):void {
if (evt.keyCode==Keyboard.SPACE){
gotoAndPlay(8);
}
}
As far as the clicking issue goes, see if the page linked below has any bearing in your situation...
Copy link to clipboard
Copied
That definitely got it to function the way I wanted, thanks.
Now I am running into a problem that I can only use this key press once.
To explain what I mean I will describe what is taking place.
I have added a function into my game that allows the player to press the space bar and an excel spreadsheet is the next frame played on the screen(an attempt to fool your boss at work).
I want the player to be able to toggle back and forth between these screens, but it only works for one cycle. Once I return back to the game, the space bar function no longer works.
Do I need to some how tell it to reset?
Copy link to clipboard
Copied
How is this coded into your timeline as far as what you call toggling?
Copy link to clipboard
Copied
I just used the word toggling as a verb for going back and forth. It has no relation to the code... the code looks like this:
function KeyPressed(evt:KeyboardEvent):void {
if (evt.keyCode==Keyboard.SPACE){
gotoAndPlay(8);
}
}
then I have a similar code in frame 8 to send it back to frame 3 that looks like this:
function KeyPressed(evt:KeyboardEvent):void {
if (evt.keyCode==Keyboard.BACKSPACE){
gotoAndPlay(3);
}
}
However, I can only do this tranistion once. If I press Space, I do go to frame 8 then if I press BackSpace, I go back to frame 3... but if I press Space again, I do not go to frame 8, it does nothing.
I want to be able to go back and forth between frames by pressing Space and BackSpace
Copy link to clipboard
Copied
Combine your keyboard control code into one function and place the code in frame 1 of a layer that extends the length of the timeline. That will make it available/active anywhere you need it.
stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyPressed);
function KeyPressed(evt:KeyboardEvent):void {
if (evt.keyCode==Keyboard.SPACE){
gotoAndPlay(8);
} else if (evt.keyCode==Keyboard.BACKSPACE){
gotoAndPlay(3);
}
}
Copy link to clipboard
Copied
First of all, thank you for your help.
Your last response did allow me to go back and forth between frames the way I want it to. However, if I press Space two times in a row (a player could do this on accident) it takes me back to frame 1. If I hit BackSpace two times in a row, it moves me forward to frame 4. I am just wondering why this would happen if it doesn't show up in the code.
Copy link to clipboard
Copied
If the intention is to actually stop at those frames, then use gotoAndStop() instead of gotoAndPlay(). If you tell something to gotoAndPlay where it already is, any stop() command you might have had in that frame is already used up, so it plays on.
Copy link to clipboard
Copied
That was it! Thank you for your help.
Copy link to clipboard
Copied
You're welcome
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more