0
votes

I'm creating a game where you as the player is holding a baseball bat and when you click the button you swing your bat. When you swing your bat you hit the enemy the enemy goes flying off in the opposite direction of where you hit them like a golf ball. I have done the moving and attacking function working but how can I register the hittest so it hits the enemy when facing towards it and the enemy going back. This is what I done so far:

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');
            }
        }
    }
}
1

1 Answers

0
votes

When the character's direction changes, change a variable depending on the way they are now facing, for example; direction = 0 when they are facing down, 1 when they are facing right, etc. Then use this and the enemy's position to work out whether or not the enemy has been hit. For making the enemy fly back, you would get the character's direction to work out which way they would fly back. I would give some example code to help explain, but I'm on my tablet.