Animation Class

Hi, I’m writng my first maze game.

I’ve based my project from the http://www.cokeandcode.com tutorials (spaceinvaders and tilemaps)

So, I’ve a Game class, Sprite, SpriteStore, Entity,Enemy,Player and Bonus

Now I want to create an animation (like cartoons) for Entity and bonus objects; I think that a good idea is to create a class like the Animation class from “new riders java game developers” book.
The Animation class will extends the sprite class and contain this methods:


public synchronized void addFrame(Image image,long duration)

Adds an image to the animation with the specified duration (time to display the image).

public synchronized void start()

Starts this animation over from the beginning.

public synchronized void update(long elapsedTime)

Updates this animation’s current image (frame), if neccesary.

public synchronized Sprite getImage()

Gets this Animation’s current image. Returns null if this animation has no images.

The sprite class is the original class from Spaceinvaders tutorial http://www.cokeandcode.com/info/showsrc/showsrc.php?src=../spaceinvaders/org/newdawn/spaceinvaders/Sprite.java

The constructor of Animation is:

 public Animation(ArrayList frames, long totalDuration) 

so, in the Sprite class I’ve add an empty constructor

What do you think?? it’s a good idea??

Yes, it’s a good idea.

I don’t know why you bothered to synchronize the methods though.

that methods were taken from “new Riders developing java game”… I don’t use threads so my methods don’t have “synchronized” (forgive me the mistake ;))

Now I’ve a small doubt: the SpriteStore class have a

public Sprite getSprite(String ref)

that return a Sprite and ref is the reference to the image to use for the sprite

The addFrame method from Animation class is:

 public void addFrame(Image image,long duration) 

so, if I extends Animation from Sprite I’ve to modify addFrame, it’s correct??

From the 2 method declarations you posted, you don’t necessarily have to change either method. It might be better if you did change one or both of them, but it’s up to you.