Jump to content
  • Advertisement
Sign in to follow this  
imtang

Animation stops after first frame

This topic is 893 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 = 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
Advertisement

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!