# Managing animations

I'm developing a 2d game that requires sprite based animations. I was thinking about which is the most convenient way to manage many of these sprites all together with timings.

I already know how they should be managed by principle but I was looking for a possibly object-oriented way to manage them all. The easy part is that I shouldn't care about which specific sprite of an animation is shown related to a position or movement of the object itself, they're just static (as far as I can tell).

I'm using Java + Processing to handle the drawing.

Any suggestions?

When I write 2d games, I tend to set it up like this:

class Sprite:
has a Vector2 for position
has a Collection (Array or Hashmap) of Viewers/Renderers (each could be a static image or an Animation that encapsulates spritesheet functionality)
has a reference to the current Viewer/Renderer to update and draw
has a State management system
has whatever other properties you need (possibly bools for horizontal and vertical flipping)


The state management system can be a FSM pattern, or a simple enum and switch statement. Its purpose is to point the current Viewer/Renderer reference to the appropriate one from the collection based on the sprite's state

I'm not familiar with Processing, so I can't much on the details of the Viewer/Renderer interface/abstract class and the static image vs sprite sheet implementations.

Check out Brandon Furtwangler's blog, it's C# and XNA, but he shows off his really solid architecture for managing sprites. His animation system is also really cool, and although it uses lambdas, you may be able to achieve similar effects with anonymous methods in Java with only slightly more verbosity.

For each animation set a period as a number of frames, and make a mapping from the frames of that period to their respective images.

Your render function should have a counter associated, for each frame rendered increment the counter, and for each animation choose the frame to draw as counter modulus period.