Advertisement

Animation stops after first frame

Started by February 07, 2016 03:26 PM
2 comments, last by Alberth 8 years, 10 months ago
I try to implement an explosion into my game but the animation stops after the first frame. If the player loses his health the explosion should be triggered. I made 3 Classes. Animation Class, an Explosion Class and GameActivity Class. I tried to only post the relevant pieces.
I searched the internet but I could not find a solution to my problem. So I am asking you if you maybe see my mistake and could help me. If more code is needed I will edit it of course. Maybe I am just overlooking something.
Animation Class{
public void setFrames(Bitmap[] frames)
{
this.frames = frames;
currentFrame = 0;
startTime = System.nanoTime();
}
public void setDelay(long d){delay = d;}
public void setFrame(int i){currentFrame= i;}
public void work()
{
long elapsed = (System.nanoTime()-startTime)/1000000;
if(elapsed>delay)
{
currentFrame++;
startTime = System.nanoTime();
}
if(currentFrame == frames.length){
currentFrame = 0;
playedOnce = true; }}
public Bitmap getImage(){
return frames[currentFrame];
}
public int getFrame(){return currentFrame;}
public boolean playedOnce(){return playedOnce; }}
This is the relevant part of the Explosion Class:
Bitmap[] image = new Bitmap[numFrames];
//Bitmap has 4 * 4 frames
for (int i = 0; i < image.length; i++) {
if (i % 4 == 0 && i > 0) row++;
image = Bitmap.createBitmap(spritesheet, (i - (4 * row)) * width,
row * height, width*4, height*4);
}
animation.setFrames(image);
animation.setDelay(10);
}
public void animating() {
if (!animation.playedOnce()) {
animation.work(); }}
If the player loses all his health I call the explosion in GameActivity
if (player.getHealth() < 0) {
// player bitmap disappears
dissapear = true;
//game ends
gameEnded=true;
explosion = new Explosion(BitmapFactory.decodeResource(getResources(),
R.drawable.images),player.getX(),player.getY()-10,9,9,25);
explosion.animating();

Few things to check, did you verify there are more than one frame?

Does it access all frames?

(would find problems in updating to the next frame)

Other direction, I don't see repaint request code, how often does it paint a frame?

Advertisement

Yes there are more than 1 frames and they are all accessed.

Every frame should be painted once.

Clearly it doesn't or it would work, wouldn't it? :)

Did you verify the repaint is actually performed for each frame?

There are also weird cases possible like all frames being the same due to some index or offset error, try painting each frame at a different spot, so you can check they are all what you expect.

This topic is closed to new replies.

Advertisement