0

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 to the x and y of the player position keeper's coordinates, 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;
            }
        }
    }
}
UnAlpha
  • 127
  • 14
  • Frames. Just put 4 animations into 4 different frames of player MovieClip and you can change its animation with a simple gotoAndStop(). – Organis Mar 25 '17 at 19:57
  • All of the animations are already animations. Each has a frame of being walking then idle, and it repeats – UnAlpha Mar 25 '17 at 21:09
  • I don't think it's possible to show you how you should design your whole game in SO over what you was already aided in previous questions - it's simply too complex topic and you are posting the same code with minor changes over and over again without real specific problem. I would advise you to first find some good guide on how to start. I recommend you to [see this tutorial](https://kerp.net/box2d/) which explain in great details how you could make a simple game. – Paweł Audionysos Mar 25 '17 at 21:38
  • @UnAlpha And your point is? You asked how to make it code-efficient. To make it code-efficient you need to organize the hero MovieClip so that it has frames with certain animations. Instead of adding/removing animation MovieClips you just navigate the hero MovieClip to show the appropriate frame, like gotoAndStop(5) = hero idle, gotoAndStop(4) = walk left, etc. You can even name the frames "idle", "up", "left" and so on, if that is more convenient than frame numbers. – Organis Mar 25 '17 at 21:39
  • @PawełAudionysos I've been asking questions since the beginning because I've started with zero knowledge of how to program and I'm working my way up. This game is simply a learning experience for me, that is all. – UnAlpha Mar 25 '17 at 22:28
  • @Organis I did not mean it in a hostile way, sorry if I made it sound like that. I did not know this way was possible, that is why I've been doing it this way. Thank you, though, I will make the appropriate changes and report some progress. – UnAlpha Mar 25 '17 at 22:28
  • Well, I can speak only for myself... I don't make to be hostile to you either. I gently advise you to see some basic tutorials first, like the one I've linked. Than if you have any specific problem ask some specific question. Because right now it's not even clear what you are asking for. – Paweł Audionysos Mar 25 '17 at 22:39
  • Basically, I wanted an alternative to using a barrage of if statements for updating my animations, because first I got to check which player you are, then what direction your going in, then add the approrpiate animation. However, with what Organis said, I think a switch statement could work because all I would have to do is check what player you are then add the animation, because all the directions of the animations would be in that movieclip. – UnAlpha Mar 25 '17 at 22:44
  • We understand that but there is a lot of ways you could do it and that is why I tell you to see some tutorials. You could also take a look at [this answer to](http://stackoverflow.com/a/42831679/2511919) for very simple example how you can have the same code used by different instances. – Paweł Audionysos Mar 25 '17 at 22:52
  • @UnAlpha While developing **anything** remotely complicated you need to understand what hierarchy is. It is very difficult to code everything on the same level, you need to make it a pyramid (metaphorically speaking). The upper level cannot and actually should not bother with how lower levels operate. Your Player should be a solid object with all the relevant data and methods inside, without **any** needs to look outside of it to operate. Then you will be able to push it around with small chunks of readable code. – Organis Mar 25 '17 at 22:57

0 Answers0