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

Simple Actionscript Help - Keypress

Guest
Nov 23, 2009 Nov 23, 2009

Hello

I am VERY new to Flash, so please excuse me if this seems very simple but all I am trying to achieve is to press a keyboard button to jump to the next frame.I have a button symobl off the stage and i am putting my code into that.

I can get it to jump to the next frame using the following code:

on (keyPress "<Left>") {
    gotoAndPlay(2);
}

but instead of the left key i want to press 'R' in order for it to jump to frame 2. I've tried using the code:

on (keyPress "r") {
    gotoAndPlay(2);
}

but this does nothing. Can anyone tell me what the correct syntax to use is.

Thanks

TOPICS
ActionScript
4.5K
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 , Nov 23, 2009 Nov 23, 2009

If you're just learning, now is as good as time as any to learn to avoid placing code "on" objects.  You can use a Key listener instead of having a button carry the load.  Put the following code in the timeline on an actions layer...

var keyListener:Object = new Object();
Key.addListener(keyListener);

keyListener.onKeyUp = function(){
      // trace(Key.getCode()); // use this to determine which key is which code
      if(Key.getCode() == 37){  // key LEFT
            trace("clicked LEFT");
      }
}

Wh

...
Translate
LEGEND ,
Nov 23, 2009 Nov 23, 2009

If you're just learning, now is as good as time as any to learn to avoid placing code "on" objects.  You can use a Key listener instead of having a button carry the load.  Put the following code in the timeline on an actions layer...

var keyListener:Object = new Object();
Key.addListener(keyListener);

keyListener.onKeyUp = function(){
      // trace(Key.getCode()); // use this to determine which key is which code
      if(Key.getCode() == 37){  // key LEFT
            trace("clicked LEFT");
      }
}

When testing in Flash, in the Flash Player you should go to the Control option in the player menubar and select the option to disable keyboard shortcuts.  Otherwise, using most keys will not work because they are associated with shortcuts.

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
Nov 23, 2009 Nov 23, 2009

Thank a lot, that worked perfectly


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 ,
Nov 23, 2009 Nov 23, 2009

You're welcome.  I should have extended my example a little bit, but hopefully you realize that the function can be used for all of the keys, just test for them...

keyListener.onKeyUp = function(){
      if(Key.getCode() == 37){  // key LEFT
            trace("clicked LEFT");
      } else if(Key.getCode() == 82){  // key "r"

            trace("clicked r");
      } etc...
}

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 ,
Nov 23, 2009 Nov 23, 2009
LATEST

Since you are new to Actionscript I would suggest that you not learn the "wrong" way to do things. Using on(event) handlers went out of style with Flash upon the introduction of Flash 6 in 2001. They have stubbornly held on, but have been removed when publishing for ActionScript 3. So don't put code directly on objects. Put it on the timeline. Here is a good tutorial about it.

http://www.quip.net/blog/2006/flash/museum-pieces-on-and-onclipevent

If you don't have many plans to keep working with Actionscript then learning AS2 is just fine, but if you want to go a long ways with Flash in the future I would recommend learning AS3.

But here is the AS2 for what you are talking about:

function onKeyDown() {
if (Key.getCode() == ("R").charCodeAt(0)) {
  gotoAndPlay(2);
  Key.removeListener(this);
}
}
Key.addListener(this);

Select the first keyframe of your timeline. Make sure you don't have any buttons or movieclips selected and open the actionscript window. Make sure it says Actionscript-Frame at the top.

This code doesn't care if it is "R" or "r" but that could be changed if it is important to you.

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