本文介绍了key_down延迟动作脚本3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按下键时第一次按下后,下一次按下是延迟的,但我不知道为什么.

When you hold down the key After first pressing the next press is delaying but I don`t know why.

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

推荐答案

要获得更快速的响应,请按以下方式进行构建:

For more responsive behaviour, structure it like this:

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延迟动作脚本3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 00:44