简体   繁体   English

AS3键盘控制

[英]AS3 Keyboard Control

I've got an as3 function that controls a movie clip with the keyboard: 我有一个as3函数,可使用键盘控制影片剪辑:

package { 包装{

import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;

public class Main_Character_Two extends MovieClip
{
    var vx:int;
    var vy:int;

    public function Main_Character_Two()
    {
        init();
    }
    function init():void
    {
        //initialize variables
        vx = 0;
        vy = 0;

        //Add event listeners
        stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
        stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
        addEventListener(Event.ENTER_FRAME, onEnterFrame);
    }
    function onKeyDown(event:KeyboardEvent):void
    {
        if (event.keyCode == Keyboard.LEFT)
        {
            vx = -5;
        }
        else if (event.keyCode == Keyboard.RIGHT)
        {
            vx = 5;
        }
        else if (event.keyCode == Keyboard.UP)
        {
            vy = -5;
        }
        else if (event.keyCode == Keyboard.DOWN)
        {
            vy = 5;
        }
    }
    function onKeyUp(event:KeyboardEvent):void
    {
        if (event.keyCode == Keyboard.LEFT || event.keyCode == Keyboard.RIGHT)
        {
            vx = 0;
        }
        else if (event.keyCode == Keyboard.DOWN || event.keyCode == Keyboard.UP)
        {
            vy = 0;
        }
    }
    function onEnterFrame(event:Event):void
    {
        //Move the player
        player.x += vx;
        player.y += vy;
    }
}

} }

This works ok but the main problem is that when you press the right key (and hold it down) then press the left key, the character will move to the left but when you release the left key (with the right key still held down) the character just stops. 可以,但是主要的问题是,当您按向右键(并按住不放)然后按向左键时,字符将向左移动,但是当您释放向左键时(仍然按住向右键)角色就停止了。 How can I make it so the character starts moving to the right again in this situation (if Im still holding the right key after Ive released the left key) 我该如何做才能使角色在这种情况下再次开始向右移动(如果在Ive释放左键之后Im仍然按住右键)

Thanks 谢谢

You could put your 'player' positioning code into an Event.ENTER_FRAME loop and use KeyboardEvent.KEY_DOWN and KeyboardEvent.KEY_UP to set booleans and position the player in the loop like: 您可以将“玩家”定位代码放入Event.ENTER_FRAME循环中,然后使用KeyboardEvent.KEY_DOWNKeyboardEvent.KEY_UP设置布尔值并将玩家放置在循环中,例如:

var leftIsPressed:Boolean = false;
var rightIsPressed:Boolean = false;
var upIsPressed:Boolean = false;
var downIsPressed:Boolean = false;
var speed:Number = 5;
var vx:Number = 0;
var vy:Number = 0;

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);

function keyDownHandler(e:KeyboardEvent):void {
    switch(e.keyCode) {
        case Keyboard.LEFT : leftIsPressed = true; break;
        case Keyboard.RIGHT : rightIsPressed = true; break;
        case Keyboard.UP : upIsPressed = true; break;
        case Keyboard.DOWN : downIsPressed = true; break;
    }
}

function keyUpHandler(e:KeyboardEvent):void {
    switch(e.keyCode) {
        case Keyboard.LEFT : leftIsPressed = false; break;
        case Keyboard.RIGHT : rightIsPressed = false; break;
        case Keyboard.UP : upIsPressed = false; break;
        case Keyboard.DOWN : downIsPressed = false; break;
    }
}

function enterFrameHandler(e:Event):void {
    vx = -int(leftIsPressed)*speed + int(rightIsPressed)*speed;
    vy = -int(upIsPressed)*speed + int(downIsPressed)*speed;
    player.x += vx;
    player.y += vy;
}

[EDIT] Added a more detailed example. [编辑]添加了更详细的示例。

Of course in this case, if both left and right keys are pressed, this would result in a 0 offset. 当然,在这种情况下,如果同时按下左右键,则偏移量为0

您可以侦听KeyboardEvent.KEY_UP来确定何时释放键,因此可以跟踪哪些键被保留。

The KeyboardEvent.KEY_DOWN event is only fired when a key goes from being in the "up" state to being in the "down" state, so your character stops after you lift your finger from the LEFT key because no new KEY_DOWN event has been triggered that would cause the character to move. 仅当按键从“向上”状态变为“向下”状态时才会触发KeyboardEvent.KEY_DOWN事件,因此,在您将手指从LEFT键上抬起后,您的角色就会停止,因为没有触发新的KEY_DOWN事件这会导致角色移动。

You might be able to accomplish what you're after by adding a KeyboardEvent.KEY_UP event that checks to see if any of the other arrow keys are being held down when the event fires, but you'd probably need to refactor your current code to avoid repeating yourself. 您可能可以通过添加KeyboardEvent.KEY_UP事件来完成操作,该事件检查事件触发时是否查看是否按住了其他箭头键,但是您可能需要将当前代码重构为避免重复自己。

Understanding this phenomenon requires a basic understanding of the Event model, which keyboard input is based upon. 要了解此现象,需要对事件模型有基本的了解,该事件是键盘输入所基于的。 It works differently in AS3 than it does in AS2. 它在AS3中的工作方式与在AS2中不同。

When you press a key, an event is fired to call a function - but it is only fired once , when the key is pressed. 当您按下某个键时,会触发一个事件以调用函数-但在按下该键时只会触发一次 The event handlers don't check whether the key is held down. 事件处理程序不检查键是否被按下。 (Try adding 5 to your object's x/y value instead of your vx in your onKeyDown function; you'll find that your character only moves when you press a key, not while you hold it.) (尝试在onKeyDown函数中将对象的x / y值加5而不是vx;您会发现角色仅在按下某个键时移动,而不是在按住该键时移动。)

Similarly, when you released a key, an event is fired to call a function - but it is only fired once, when the key is actively released. 同样,释放键时,会触发一个事件以调用函数-但在主动释放键时,该事件仅触发一次。 No events are fired when the key is just sitting there. 当钥匙正坐在那里时,不会触发任何事件。

Building upon this...can you find the source of your current problem? 在此基础上...您能找到当前问题的根源吗?

function onKeyUp(event:KeyboardEvent):void
{
    if (event.keyCode == Keyboard.LEFT || event.keyCode == Keyboard.RIGHT)
    {
        vx = 0;
    }
    else if (event.keyCode == Keyboard.DOWN || event.keyCode == Keyboard.UP)
    {
        vy = 0;
    }
}

If you didn't find it, it's this block. 如果找不到,就是这个方块。

    if (event.keyCode == Keyboard.LEFT || event.keyCode == Keyboard.RIGHT)
    {
        vx = 0;
    }

This statement tells your program to set velocity equal to zero if either the right or the left keys are pressed. 该语句告诉您的程序,如果按下向右或向左键,则将速度设置为零。 However, since Flash only checks for a keypress when you first press the key , holding down a key will not trigger your onKeyDown function. 但是,由于Flash 仅在您首次按下该键时才检查是否有按键 ,因此按住键不会触发onKeyDown函数。 Hence, if you hold both left and right, and let one go, you're setting your velocity to zero. 因此,如果同时握住左手和右手,然后放开手,则将速度设置为零。

Corey has the logical solution here, and there are some other reasons that you'll want to use a toggled boolean value to check keyboard input (the main reason is that you won't get any repeat delay). Corey在这里有一个合理的解决方案,还有其他一些原因,您需要使用切换的布尔值来检查键盘输入(主要原因是您不会得到任何重复延迟)。

I hope this answer cleared it up for you a little - a little knowledge can lead to great things. 我希望这个答案对您有所帮助-一点点知识就能带来美好的事物。 Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM