I am adding bitmap animations into my simple 2D android game engine(if you can call it one yet.. :D) and I am facing problem of cycling through frames at pre defined FPS/rate.
What I have done for now is simply use System to get currentTimeMillis and use it as reference but I am pretty sure that’s not the best decision.. I was thinking I could pass current frame to the drawing method and use that to the drawing method.
Currently I have object that you get the drawable from and I use it on renderer like this:
canvas.drawBitmap(animation.getDrawable(), (int)x, (int)y, null);
and getDrawable() returns current frame of the animation or the only frame if its still image and heres the code
if(frames.size() > 1) {
long current = System.currentTimeMillis();
if (currentFrame == frames.size() - 1 && frameDirection == 1) {
frameDirection = -1;
} else if (currentFrame == 0 && frameDirection == -1) {
frameDirection = 1;
}
if ((current - lastFrame) > delay) {
currentFrame += frameDirection;
lastFrame = current;
}
}
return frames.get(currentFrame);
and in my eyes that just looks so bad and unefficient.. What can I do about this? Is the only way to go passing current frame to getDrawable or how should this be done?
If I understand this right, you want to create an animation class for you game engine?
In my Game Engine I’ve got a class called
SpriteSheetAnimation, which basically iterates through every frame of the animation when the update method is called – I’m sure you’ll understand the logic behind it and you’ll find a way to use it:I hope this helps you and gets you started.