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

Using a Keyboard Event to play a different scene

Guest
Mar 20, 2011 Mar 20, 2011

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?

TOPICS
ActionScript
4.9K
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

LEGEND , Mar 21, 2011 Mar 21, 2011

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.

Translate
Guest
Mar 20, 2011 Mar 20, 2011

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.

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
LEGEND ,
Mar 20, 2011 Mar 20, 2011

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...

http://kb2.adobe.com/cps/155/tn_15586.html

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
Guest
Mar 20, 2011 Mar 20, 2011

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?

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
LEGEND ,
Mar 20, 2011 Mar 20, 2011

How is this coded into your timeline as far as what you call toggling?

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
Guest
Mar 21, 2011 Mar 21, 2011

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

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
LEGEND ,
Mar 21, 2011 Mar 21, 2011

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);

    }
}

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
Guest
Mar 21, 2011 Mar 21, 2011

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.

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
LEGEND ,
Mar 21, 2011 Mar 21, 2011

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.

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
Guest
Mar 21, 2011 Mar 21, 2011

That was it! Thank you for your help.

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
LEGEND ,
Mar 21, 2011 Mar 21, 2011
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