Sign in to follow this  

Animation stops after first frame

This topic is 679 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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[i] = 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();

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

This topic is 679 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this