samedi 25 mars 2017

How can I efficiently update animations in my game?

For each character skin, it has 4 MovieClips corresponding to each possible direction (up, down, left, and right).

Example:

For my Player 1 skin, it would be like this.

Player 1 Up Animation (P1UAnim_mc)

Player 1 Down Animation (P1DAnim_mc)

Player 1 Left Animation (P1LAnim_mc)

Player 1 Right Animation (P1RAnim_mc)

Now, the way my movement works is we have a Movieclip that matches the size of the animations (they don't change in size), and this Movieclip is invisible, and this is what moves when we tell it to (I call it a player position keeper). This is done by clicking a direction on the DPAD, where we then move the player in the appropriate direction with the coressponding walkspeed for that direction, then we check if we hit something in my array of obstacles. If we did, we move the player back.

So now that we have that out of the way, I need to have animations that correspond to the direction and character skin the player is using/going in.

One idea I had, had an event listener for every time we enter a frame, where we check what charSkin the player is using, then check the direction, and then add the appropriate animation. We do not need to update the animations x,y coordinates because in their class file it is always updating itself, and knows when to remove itself. So all I need to do is find the appropriate time to add it and leave the rest to the animation's class.

My problem with this technique I tried was the code was really difficult to understand, it was a barrage of if else statements checking the factors mentioned above.

Here is my MovementReworked class which does not include the animations, so that you guys can actually read it. If you want me to edit the post and add the Movement class with sloppy animations code as well, I will, but it's pretty unreadable.

package 
{
    import flash.display.Stage;
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.TouchEvent;
    import flash.net.dns.AAAARecord;
    import flash.ui.Multitouch;
    import flash.ui.MultitouchInputMode;


    public class MovementReworked extends MovieClip
    {
        public function MovementReworked(main:Game)
        {
            // I will be changing these addChilds in the future
            // Just ignore it for now

            Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;

            addChild(Game.playerPosKeeper_mc);
            Game.playerPosKeeper_mc.x = 384;
            Game.playerPosKeeper_mc.y = 46;

            addChild(main.up_dpad);
            main.up_dpad.x = 55;
            main.up_dpad.y = 336;

            addChild(main.down_dpad);
            main.down_dpad.x = 57;
            main.down_dpad.y = 432;

            addChild(main.left_dpad);
            main.left_dpad.x = 19;
            main.left_dpad.y = 372;

            addChild(main.right_dpad);
            main.right_dpad.x = 118;
            main.right_dpad.y = 372;

            addChild(main.menu_dpad);
            main.menu_dpad.x = 61;
            main.menu_dpad.y = 377;

            addChild(main.run_dpad);
            main.run_dpad.x = 684;
            main.run_dpad.y = 369;

            addChild(main.barrierRoof1_game);
            main.barrierRoof1_game.x = 0;
            main.barrierRoof1_game.y = 0;

            addChild(main.barrierRoof2_game);
            main.barrierRoof2_game.x = 0;
            main.barrierRoof2_game.y = 470;

            addChild(main.barrierRoof3_game);
            main.barrierRoof3_game.x = 0;
            main.barrierRoof3_game.y = 320;

            addChild(main.barrierSide1_game);
            main.barrierSide1_game.x = 0;
            main.barrierSide1_game.y = 0;

            addChild(main.barrierSide2_game);
            main.barrierSide2_game.x = 790;
            main.barrierSide2_game.y = 0;
            // I will be changing these addChilds in the future

            for each (var aButton:MovieClip in main.Buttons)
            {
                aButton.addEventListener(TouchEvent.TOUCH_BEGIN, onDown);
                aButton.addEventListener(TouchEvent.TOUCH_OUT, onUp);
                aButton.addEventListener(TouchEvent.TOUCH_END, onUp);
            }

            function onDown(e:TouchEvent):void
            {
                switch (e.currentTarget)
                {
                    case main.up_dpad :
                        Game.dir = 1;
                        Game._Direction.x = 0;
                        Game._Direction.y = Game.upWalkspeed;
                        break;
                    case main.down_dpad :
                        Game.dir = 2;
                        Game._Direction.x = 0;
                        Game._Direction.y = Game.downWalkspeed;
                        break;
                    case main.left_dpad :
                        Game.dir = 3;
                        Game._Direction.x = Game.leftWalkspeed;
                        Game._Direction.y = 0;
                        break;
                    case main.right_dpad :
                        Game.dir = 4;
                        Game._Direction.x = Game.rightWalkspeed;
                        Game._Direction.y = 0;
                        break;
                }
                if (Game.idle)
                {
                    Game.idle = false;
                    addEventListener(Event.ENTER_FRAME, onFrame);
                }
            }
            function onFrame(e:Event):void
            {
                movePlayer(Game._Direction.x, Game._Direction.y);
            }

            function onUp(e:TouchEvent):void
            {
                Game.idle = true;
                removeEventListener(Event.ENTER_FRAME, onFrame);
            }

            function movePlayer(movementX:Number, movementY:Number):void
            {
                var originalX:Number = Game.playerPosKeeper_mc.x;
                var originalY:Number = Game.playerPosKeeper_mc.y;
                Game.playerPosKeeper_mc.x +=  movementX;
                if (checkCollision())
                {
                    Game.playerPosKeeper_mc.x = originalX;
                }
                Game.playerPosKeeper_mc.y +=  movementY;
                if (checkCollision())
                {
                    Game.playerPosKeeper_mc.y = originalY;
                }
            }

            function checkCollision():Boolean
            {
                for each (var StageCollisions:MovieClip in main.StageCollisions)
                {
                    if (Game.playerPosKeeper_mc.hitTestObject(StageCollisions))
                    {
                        return true;
                        Game.idle = true;
                    }
                }
                return false;
            }
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire