Okay, this is what I have done and might work nicely in your situation. I use passive rendering as well with a Timer that updates stuff about 20 times per second.
First I have a class that represents an active/playing animation. Note the code snippets are pseuo-code:
abstract class Animation {
Image getImageToDraw(); // get stuff to draw. Called from paint().
boolean isComplete(); // return true if animation is completed
boolean advance(); // potentially advance the animation. Called every timer tick. Return true if animation has advanced and repaint is required.
}
Then in your main Panel you store all running animations:
List[Animation] animations;
Your timer tick might be (assuming the callback is in your main Panel class):
public void callback() {
remove all animations where anim.isComplete() == true
for (Animation anim in animations) {
if (anim.advance()) {
repaint();
}
}
}
And paint() appears as such:
public void paint(Graphics g) {
super(g);
for (Animation anim in animations) {
g.draw(anim.getImageToDraw());
}
}
Finally you need to create an implementation of Animation. Let’s assume you have a death animation that consists of 8 images and you want it to play over the course of 2 seconds.
class DeathAnimation extends Animation {
private boolean isDone = false;
private long start_ms;
private List[Image] images;
private int i = 0;
DeathAnimation() {
initialize all your images
start_ms = System.currentTimeMillis();
}
Image getImageToDraw() {
return images[i];
}
boolean isComplete() {
return isDone;
}
boolean advance() {
long elapsed_ms = System.currentTimeMillis() - start_ms;
int calcI = elapsed_ms / 250; // advance animation 4 frames per sec
if (calcI >= 8) {
calcI = 7;
isDone = true;
}
if (i != calcI) {
i = calcI;
return true;
}
return false; // no repaint needed
}
}
So basically, your code maintains a list of running animations. Each game tick (40 msec) you iterate through them and see if you need to advance the animation. The advance also determines if the animation is complete.
The above code will encapsulate your ‘coordinate byte’ within the Animation class itself. It also properly advances the animation at a desired frame-rate independent of the machine speed.
If you want to spawn a new animation all your need to do is instantiate it and add it to the list e.g. animations.add(new DeathAnimation());
This has worked well for me in that my animations are independent of my main Panel class and I’ve found it really easy to add new ones. The above code is pretty rough and you may need to adjust it a lot for your game but I hope it gives you the general idea.