开发者

I need to add a KeyboardEvent to a movie clip in Flash (AS 3.0)

开发者 https://www.devze.com 2022-12-27 16:08 出处:网络
I have a movieclip called keyCButton that I want to add a keyboardEvent (\"C\" Key) to. This will play an animation from frames 2-30 with a sound.

I have a movieclip called keyCButton that I want to add a keyboardEvent ("C" Key) to. This will play an animation from frames 2-30 with a sound. I have watched a few tutorials but still haven't gotten the correct information to make it work. The following is my code.

stage.addEventListener(KeyboardEvent.KEY_D开发者_JAVA技巧OWN, cNote);
function cNote(event:KeyboardEvent):void
{
  if (event.keyCode == Keyboard.C)
  {
    keyCButton.gotoAndPlay(2)
  }
}

at frame 30 the code tells it to stop and then go back to frame 1

This works for the MouseEvent.CLICK but the KeyboardEvent does nothing. It returns this error for the if condition

1119: Access of possibly undefined property C through a reference with static type Class.

When I try it with the SPACE instead of C i don't get errors but it still doesn't work

Current Project Link


Keyboard.C doesn't work (don’t know why) so you have to know the key code for the C key:


stage.addEventListener(KeyboardEvent.KEY_DOWN, cNote);
function cNote(event:KeyboardEvent):void {
    //you can trace event.keyCode to get the pressed key value;
    trace(event.keyCode);
    //in your case the C key = 67
    if (event.keyCode==67) {
        keyCButton.gotoAndPlay(2);
    }
}


This may help flash to recognize your keyboard. When you Publish preview with shift+enter, go to the options in the flash player, under the control tab, and then disable keyboard shortcuts. This makes sure that Flash CS4 (or whatever you use) sends the shortcuts to the player instead of trying to hotkey the tools. Hope it helps.

-Solarcloud


Below code works perfectly:

stage.addEventListener(KeyboardEvent.KEY_DOWN, cNote);

function cNote(e:KeyboardEvent):void {
    if (e.keyCode==Keyboard.C) {
        keyCButton.gotoAndPlay(2);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消