How do I run a gif once and stop in Java?

Basically, I have a game where I want to play a gif burst whenever the player (or enemy) dies, but I want the gif to just play once. I have everything created in the class and I have an ArrayList that adds explosions to it when I need it, and now I have it to remove them as soon as the gif duration is reached (using System.currentTimeMillis ( )), The only problem is that it is not accurate and sometimes the gif stops early and disappears and sometimes rolls over, both situations cause the next one to run where the other left off. Is there some method, class, listener of some kind, or something I can use to indicate which frame my gif is in, or how many times it loops? My code can do the rest of the work,I just need something better to insert into the if statement than this:

g2D.drawImage(explosion, x - 150, y - 150, null);
    if(System.currentTimeMillis() - startingTime > 520){
        System.out.println(System.currentTimeMillis());//for testing accuracy
        Game.explosions.remove(this);
        explosion = null;
    }

      

+3


source to share


1 answer


The usual solution to this is not to use gif :)

Most games will store each gif "frame" separately and display them in order, instead of trying to get the gif rendering to work.



System.currentTimeMillis () is not overly precise, plus there can be a lot of things happening in the background between calls to your paint function (all of which affect frame rates), so I would recommend not relying on this altogether.

+6


source







All Articles