key_down的延时动作脚本3(key_down delay action script 3)

2019-10-20 02:22发布

当您按住键先按下一压榨推迟,但我不`吨知道为什么了。

           public function KeyboardControl(event:KeyboardEvent):void
    {
        keySpeed=5

        if(event.keyCode==Keyboard.RIGHT||event.keyCode==Keyboard.D)
        {
            if(_Vx<0)
            {
                _Vx=0;
            }
            _Vx += (keySpeed/mass);
        }
        if(event.keyCode==Keyboard.LEFT||event.keyCode==Keyboard.A)
        {
            if(_Vx>0)
            {
                _Vx=0;
            }
            _Vx -= (keySpeed/mass);
        }
             }

Answer 1:

对于更敏感的行为,它的结构是这样的:

private var _Vx:Number = 0;
private var keySpeed:Number = 5;
private var mass:Number = 10;

private var isKeyDown:Boolean;

private function init():void
{
  addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
}

private function addedToStageHandler(event:Event):void
{
  removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);

  addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
  addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
  addEventListener(Event.ENTER_FRAME, update);
}

// Set the direction according to the key pressed
private function keyDownHandler(event:KeyboardEvent):void
{
  isKeyDown = true;

  if(event.keyCode == Keyboard.RIGHT || event.keyCode == Keyboard.D)
  {
    directionX = 1;
  }
  if(event.keyCode == Keyboard.LEFT || event.keyCode == Keyboard.A)
  {
    directionX = -1;
  }
}

private function keyUpHandler(event:KeyboardEvent):void
{
  isKeyDown = false;
}

// Game loop
private function update(event:Event):void
{
  if(isKeyDown)
  {
    if(_Vx<0)
    {
      _Vx=0;
    }
    _Vx += (keySpeed/mass) * directionX;
  }
}


文章来源: key_down delay action script 3