动作脚本3--尝试解决在按键时出现的延迟. [英] Actionscript 3-- Trying to fix a delay that is occuring upon key press.

查看:74
本文介绍了动作脚本3--尝试解决在按键时出现的延迟.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的游戏设置键盘控制并遇到一个有趣的障碍:当玩家按下某个键向特定方向移动时,发生的物理移动延迟类似于编辑文本时发生的延迟.

I'm trying to setup keyboard control for my game and ran into an interesting obstacle: When the player presses a key to move in a specific direction a delay in the physical movement occurs similar to the delay that happens when editing text.

例如,当您按住"a"键时(仅作为示例,当然可以是任何键),在光标随后注册"aaaaaaa"之前还有第二个延迟.这里也发生同样的问题,因此,当按下方向键时,帧动画将在物理运动开始之前开始.这导致动画看起来像角色在原地运行,然后在大约1或2秒后终于开始移动.

For instance, When you hold down the "a" key (just as an example, of course it could be any key) and there is a second delay before the cursor will then register "aaaaaaa". The same problem is happening here, so when a direction key is pressed the frame animations begin before the physical movement starts. Which results in an animation that looks like the character is running in place and then finally starts moving after about a 1 or 2 seconds.

任何对修复的想法,想法或建议将不胜感激.预先谢谢大家.

Any thoughts, ideas, or advice on a fix would be much appreciated. Thanks in advance, all.

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


   }

推荐答案

为每个方向设置布尔值,并更改onKeyPress和onKeyRelease来设置它们,然后检查它们并将字符移动到onEnterThisFrame函数中.

Have bools for each direction and change your onKeyPress and onKeyRelease to set these, then check them and move your character in your onEnterThisFrame function.

这篇关于动作脚本3--尝试解决在按键时出现的延迟.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆