A step beyond Animation

Animation is key to any 2D Game, but one aspect I didn’t want to overlook was how to make switching between different animations by the direction in which a sprite is moving. To me, this logic is repeated again and again in a 2D Game. You move your character across the screen, then back again. If your character walks (or runs) from left to right across the screen, then he moves back (from right to left), you either have him run backward or change his direction. The variance is in how many directions the character can go. Does the character only move in one direction? Is he able to only move left and right? Can he jump? Does he move in four directions or eight? No matter which kind of game you make, your sprites will move in some direction, likely multiple directions.

I feel that this is another item to be added to the framework. The hard part was to figure out the names of the classes. My first thought was Movement, which is confusing with the sprite actually moving, so I came up with the class name of Motion. What the Motion class does is encapsulate the transitioning of Animations based on the direction in which the Sprite moves. If you remember in my third video, making the Animations switch between directions can get excessive. When the sprite moves from left to right, it uses the Animation for that direction, and when it moves from right to left it uses the Animation for that direction. Since the Motion classes internally use Animation objects, the developer is free to put in any type of Animation class they want, including using the same Animation.

Now, you can have a sprite that has a looping Animation when moving from down to up and a static (non-changing) Animation when moving from up to down. If you decide to change how the sprite will be animated, you just have to change the Animation used in the Motion class. This makes changes easier and gives the flexibility to experiment with different animation sequences.

A Note about the Animation and Motion classes is that they don’t deal with placing the image on the screen. The location where these images are placed must be passed into the draw method. The Motion forwards that location to the Animation class. The Motion class has an additional method for setting the direction, which it will use to determine which Animation class to use for rendering.