0

我试图通过按键从库中播放一个moveiclip,我已经链接了它,但它仍然不起作用(这只是代码的一部分,有事件侦听器和处理程序,角色移动正常)

stage.addEventListener(Event.ENTER_FRAME, Move);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPress);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);

var aP:Boolean = false;
var dP:Boolean = false;
//creating a new Character_right instance
var character_right:Character_right = new Character_right(); 

function Move(vet:KeyboardEvent)
{
    if(aP)
    {
        char.x -= 5;
        char.scaleX = -0.55;
    }
    if(dP)
    {
        char.x += 5;
        char.scaleX = -0.55;
    }
}

function keyPress(evt:KeyboardEvent)
{
    switch(evt.keyCode)
    {
        case Keyboard.A:
        {
        aP = true;
        break;
        }
        case Keyboard.D:
        {
        dP = true;
        character_right.play(); 
        break;
        }
     }
}

function keyUp(evt:KeyboardEvent)
{
        switch(evt.keyCode)
    {
        case Keyboard.A:
        {
        aP = false;
        break;
        }
        case Keyboard.D:
        {
        dP = false;
        break;
        }
     }
}
4

2 回答 2

0

在您的代码中,您正在执行函数Character_right.play()afterbreak并 break 停止执行其余的循环代码,因此执行Character_right.play()before break,例如:

function keyPress(evt:KeyboardEvent)
{
    switch(evt.keyCode)
    {
        case Keyboard.A:
        {
        aP = true;

        break;
        }
        case Keyboard.D:
        {
        dP = true;
        Character_right.play();
        break;
        }
     }
}
于 2013-12-18T19:34:24.243 回答
0

检查这篇文章,有一个关于键盘事件的很好的教程,我相信这对你理解这个概念和实现你的逻辑很重要。

简而言之,它类似于以下示例,但请记住为您的范围实现所有必要的逻辑。

        var isRight:Boolean;
        var isLeft:Boolean;
        var isUp:Boolean;
        var isDown:Boolean;
        //creating a new Character_right instance
        var character_right:Character_right = new Character_right(); 

        stage.addEventListener(KeyboardEvent.KEY_DOWN, downKeyHandler, false, 0, true);
        function downKeyHandler(event:KeyboardEvent):void
        {
            if (event.keyCode == Keyboard.RIGHT )
            {
                isRight = true;
            }
            if (event.keyCode == Keyboard.LEFT)
            {
                isLeft = true;
            }
            if (event.keyCode == Keyboard.UP)
            {
                isUp = true;
            }
            if (event.keyCode == Keyboard.DOWN)
            {
                isDown = true;
            }
        }

        stage.addEventListener(KeyboardEvent.KEY_UP, upKeyHandler, false, 0, true);
        function upKeyHandler(event:KeyboardEvent):void
        {
            if (event.keyCode == Keyboard.RIGHT)
            {
                isRight = false;
            }
            if (event.keyCode == Keyboard.LEFT)
            {
                isLeft = false;
            }
            if (event.keyCode == Keyboard.UP)
            {
                isUp = false;
            }
            if (event.keyCode == Keyboard.DOWN)
            {
                isDown = false;
            }
        }

        stage.addEventListener(Event.ENTER_FRAME, loopHandler, false, 0, true);
        function loopHandler(event:Event):void
        {
            if (isRight)
            {
                //do something here…
                                    character_right.play();
                            }
            if (isLeft)
            {
                //do something here...
            }
            if (isUp)
            {
                //do something here...
            }
            if (isDown)
            {
                //do something here...
            }
        }
于 2013-12-18T10:06:37.310 回答