Actionscript 3检测连续按键

时间:2013-05-15 16:04:15

标签: actionscript-3

我有2个电影剪辑。一个人走路,另一个跑步。如何按下左箭头键,它将播放行走剪辑。如果我快速连续按下左箭头,它将切换到正在运行的剪辑。我正在使用Actionscript 3。

1 个答案:

答案 0 :(得分:0)

这需要一些代码工作,因为Flash AS3没有双击的原生支持。以下是如何使用SetTimeout

实现此效果的简单示例
var keyCodeToListenFor = '13';  // can be any keycode;
var keySinglePress:Boolean = false;
var keyDoublePress:Boolean = false;
var timeToWaitForDoublePress:Number = 400;
var waitingForDoublePress:Boolean = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN, handleKeyDown);


function handleKeyDown(e:Event) {

   if (e.keyCode == keyCodeToListenFor) {

       keySinglePress = true;
       if (waitingForDoublePress) {
           keyDoublePress = true;
       }

       waitingForDoublePress = true;

       setTimeout(function() { 
           waitingForDoublePress = false; 
           keyDoublePress = false;
       }, timeToWaitForDoublePress);


   }

   if (keyDoublePress) {
       trace('You logged a double press!')
   } else if (keySinglePress)   {
       trace('You logged a single press!');
   }
}