Animation Effects

Imaging effects in my original Old Style Game Framework was more “involved” to get it to work. I had to make it as an added feature of the Animation class, by creating the ImageFx class. Effects that dealt with color changes; brightening, darkening, changing color, etc, involved more complicated procedures and using a BufferedImageOp object to do the operation. Rotating the image involved using the AffinsTransform class. One thing about this class is that you had to be careful where the “center” of the rotation is; if you move the object and then try to rotate it, you get a different effect than rotating it and then moving.

LibGDX, with the underlying LWJGL framework underneath it, makes much of this trivial. The SpriteBatch’s draw method has arguments for rotating, shrinking and enlarging, cropping and even flipping the image horizontally and/or vertically.

public void draw(TextureRegion region, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation)

public void draw(Texture texture, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation, int srcX, int srcY, int srcWidth, int srcHeight, boolean flipX, boolean flipY)

The use of the booleans to flip the image can also be achieved by using the method that takes U and V values and reversing them. I demonstrated this in my second video on still image rendering. Coloring and translucency effects are done by setting the color on the SpriteBatch.

SpriteBatch.setColor(new Color(red, green, blue, translucency or alpha));

In my framework, I integrated this within the BaseAnimation class. Using a framework, takes away all the hard work in making a game.

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.