角色跳动画

时间:2013-05-24 23:06:50

标签: actionscript-3 flash animation

我试图创建一个平台游戏,但我正在努力与游戏的跳跃部分。我有跳跃工作,但当我尝试添加动画时,它出错了。当我按下跳跃按钮时它跳跃但是当我着陆时它会卡在跳跃动画的结尾,直到我移动。其次,当我跳跃并按下向右或向左按钮时,它跳跃但播放动画动画,而我试图在半空中移动。我该如何解决这些问题

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

    public class Player extends MovieClip
    {
        //Player run speed setting
        var RunSpeed:Number = 8;
        //Player key presses
        var RightKeyPress:Boolean = false;
        var LeftKeyPress:Boolean = false;
        var UpKeyPress:Boolean = false;
        //Jump variables
        var Gravity:Number = 1.5;
        var Yvelocity:Number = 0;
        var CanJump:Boolean = false;

        public function Player()
        {
            // constructor code
            stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyPressed);
            addEventListener(Event.ENTER_FRAME, Update);
            stage.addEventListener(KeyboardEvent.KEY_UP, KeyReleased);
        }

        function KeyPressed(event:KeyboardEvent)
        {
            //When Key is Down
            if (event.keyCode == 39)
            {
                RightKeyPress = true;
            }

            if (event.keyCode == 37)
            {
                LeftKeyPress = true;
            }

            if (event.keyCode == 38)
            {
                UpKeyPress = true;
            }
        }

        function Update(event:Event)
        {
            //Adding gravity to the game world
            Yvelocity +=  Gravity;
            //if player is more than 300 on the y-axis
            if (this.y > 300)
            {
                //Player stays on the ground and can jump
                Yvelocity = 0;
                CanJump = true;
            }

            if ((RightKeyPress == true))
            {
                x +=  RunSpeed;
                gotoAndStop('Run');
                scaleX = 1;
            }
            else if ((LeftKeyPress == true))
            {
                x -=  RunSpeed;
                gotoAndStop('Run');
                scaleX = -1;
            }

            if ((UpKeyPress == true && CanJump))
            {
                Yvelocity = -15;
                CanJump = false;
                gotoAndStop('Jump');
            }
            this.y +=  Yvelocity;
        }

        function KeyReleased(event:KeyboardEvent)
        {
            if (event.keyCode == 39)
            {
                event.keyCode = 0;
                RightKeyPress = false;
                gotoAndStop('Idle');
            }

            if (event.keyCode == 37)
            {
                event.keyCode = 0;
                LeftKeyPress = false;
                gotoAndStop('Idle');
            }

            if (event.keyCode == 38)
            {
                event.keyCode = 0;
                UpKeyPress = false;
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

this.y> 300,这是你的跳跃结束逻辑。添加gotoAndStop('空闲');在 if 的阻止。

答案 1 :(得分:1)

在执行这些块之前,只需检查左右按键上的canJump是否为真。如果canJump为true则允许运行的东西