0
我試圖爲我的遊戲設置鍵盤控制,並遇到了一個有趣的障礙:當玩家按下某個鍵在特定方向上移動時,物理移動的延遲發生類似於編輯文本時發生的延遲。例如,按住「a」鍵(當然,例如當然可以是任何鍵),並且在光標將註冊「aaaaaaa」之前還有第二個延遲時間。同樣的問題發生在這裏,所以當方向鍵被按下時,框架動畫在物理運動開始之前就開始了。這導致動畫看起來像角色正在運行,然後在大約1或2秒後最終開始移動。Actionscript 3--試圖解決按鍵時發生的延遲。
任何想法,想法或修復建議將不勝感激。在此先感謝,所有。
import flash.events.Event;
import flash.events.KeyboardEvent;
character.stop();
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPress);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyRelease);
stage.addEventListener(Event.ENTER_FRAME, onEnterThisFrame);
var moving:int = 4;
var animate:Boolean = false;
function onKeyPress(e:KeyboardEvent):void
{
switch(e.keyCode)
{
case 37: moving = 1; character.gotoAndStop(6); character.x-=5; break; //left
case 38: moving = 2; character.gotoAndStop(4); character.y-=5; break; //up
case 39: moving = 3; character.gotoAndStop(8); character.x+=5; break; //right
case 40: moving = 4; character.gotoAndStop(2); character.y+=5; break; //down
case 32: handleAttack();
}
animate = false;
}
function onKeyRelease(e:KeyboardEvent):void
{
switch(moving)
{
case 1: character.gotoAndStop(6); break; //left
case 2: character.gotoAndStop(4); break; //up
case 3: character.gotoAndStop(8); break; //right
case 4: character.gotoAndStop(2); break; //down
}
animate = true;
}
function handleAttack():void
{
switch (moving)
{
case 1: character.gotoAndStop(11); break; //left
case 2: character.gotoAndStop(10); break; //up
case 3: character.gotoAndStop(12); break; //right
case 4: character.gotoAndStop(9); break; //down
}
}
function onEnterThisFrame(e:Event):void
{
if (animate == true)
{
switch (moving)
{
case 1: if(character.currentFrame == 6) character.gotoAndStop(5); break;
case 2: if(character.currentFrame == 4) character.gotoAndStop(3); break;
case 3: if(character.currentFrame == 8) character.gotoAndStop(7); break;
case 4: if(character.currentFrame == 2) character.gotoAndStop(1); break;
}
}
}
爲什麼不使用輸入框作爲遊戲的主循環?在你的代碼中,在「輸入框架」中,你只能管理運動狀態...... –