0

我正在创建一个游戏,你作为玩家拿着棒球棒,当你点击按钮时,你挥动你的球棒。当你挥动你的球棒时,你击中了敌人,敌人会像高尔夫球一样朝你击中他们的相反方向飞去。我已经完成了移动和攻击功能的工作,但是我怎样才能注册命中测试,以便它在面对敌人时击中敌人并且敌人向后退。这是我到目前为止所做的:

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

    public class Player extends MovieClip
    {
        var walkSpeed:Number = 4;
        var walkRight:Boolean = false;
        var walkLeft:Boolean = false;
        var walkUp:Boolean = false;
        var walkDown:Boolean = false;
        var attacking:Boolean = false;

        public function Player()
        {
            stage.addEventListener(KeyboardEvent.KEY_DOWN , walk);
            addEventListener(Event.ENTER_FRAME, Update);
            stage.addEventListener(KeyboardEvent.KEY_UP, stopWalk);
            stage.addEventListener(MouseEvent.CLICK, attack);
        }

        function walk(event:KeyboardEvent)
        {

            if (event.keyCode == 68)
            {
                walkRight = true;
            }
            if (event.keyCode == 87)
            {
                walkUp = true;
            }
            if (event.keyCode == 65)
            {
                walkLeft = true;
            }
            if (event.keyCode == 83)
            {
                walkDown = true;
            }
        }

        function Update(event:Event)
        {
            if (attacking == true)
            {
                walkRight = false;
                walkLeft = false;
                walkUp = false;
                walkDown = false;
            }
            else if (attacking == false)
            {
                var dx = parent.mouseX - x;
                var dy = parent.mouseY - y;
                var angle = Math.atan2(dy,dx) / Math.PI * 180;
                rotation = angle;

                if (walkRight == true)
                {
                    x +=  walkSpeed;
                    gotoAndStop('walk');
                }
                if (walkUp == true)
                {
                    y -=  walkSpeed;
                    gotoAndStop('walk');
                }
                if (walkLeft == true)
                {
                    x -=  walkSpeed;
                    gotoAndStop('walk');
                }
                if (walkDown == true)
                {
                    y +=  walkSpeed;
                    gotoAndStop('walk');
                }
            }


        }

        function stopWalk(event:KeyboardEvent)
        {
            if (attacking == false)
            {
                if (event.keyCode == 68)
                {
                    event.keyCode = 0;
                    walkRight = false;
                    gotoAndStop('stance');
                }
                if (event.keyCode == 87)
                {
                    event.keyCode = 0;
                    walkUp = false;
                    gotoAndStop('stance');
                }
                if (event.keyCode == 65)
                {
                    event.keyCode = 0;
                    walkLeft = false;
                    gotoAndStop('stance');
                }
                if (event.keyCode == 83)
                {
                    event.keyCode = 0;
                    walkDown = false;
                    gotoAndStop('stance');
                }
            }
        }

        function attack(event:MouseEvent)
        {
            if (attacking == false)
            {
                attacking = true;
                gotoAndStop('attack');
            }
        }
    }
}
4

1 回答 1

0

当角色的方向改变时,根据他们现在面对的方式改变一个变量,例如;direction = 0 当他们朝下时,1 当他们朝右时,等等。然后使用这个和敌人的位置来计算敌人是否被击中。为了让敌人飞回来,你会得到角色的方向来确定他们会飞回的方向。我会给出一些示例代码来帮助解释,但我在我的平板电脑上。

于 2013-04-05T00:17:27.447 回答