如何使gif运行一次并在Java中停止? [英] How to make a gif run once and stop in Java?

查看:295
本文介绍了如何使gif运行一次并在Java中停止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我有一个游戏,想要在玩家(或敌人)死亡时播放gif爆炸,但是我希望gif只播放一次。
我已经在一个类中设置了所有内容,并且我有一个ArrayList可以在需要时添加爆炸。目前,我已经将其设置为在达到gif的持续时间后将其删除(使用System。 currentTimeMillis())。唯一的问题是,它不完全正确,有时gif会提前停止并消失,有时会翻转,这两种情况都会导致下一个开始,而另一个停止。
是否存在某种方法,类,某种侦听器,或者可以用来告诉我gif处于哪个帧或已循环多少次的任何东西?我的代码可以完成其余的工作,我只需要在if语句中添加以下内容:

Basically I have a game where I want to play an explosion gif whenever the player (or an enemy) dies, but I want the gif to just play once. I have everything set up in a class and I have an ArrayList that gets explosions added to it when I need it to, and I currently have it set up to remove them once the gif's duration has been reached (using System.currentTimeMillis()). The only problem there is that it isn't exact, and sometimes the gif stops early and disappears, and sometimes it rolls over, both situations causing the next one to start where the other one left off. Is there some sort of method, class, listener of some sort, or anything I can use to tell what frame my gif is on, or how many times it has looped? My code can do the rest of the work, I just need something better to put in my 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;
    }


推荐答案

通常的解决方法是不使用gif:)

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

大多数游戏会分别存储gif的每个框架,并按顺序显示它们,而不是试图使gif渲染正常工作。

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

System.currentTimeMillis()并不十分准确,此外,在两次调用draw函数之间,可能还会有很多东西在后台运行(所有这些都会影响帧速率),因此我建议您不要再依赖于此。

System.currentTimeMillis() is not terribly accurate, plus there can be tons of stuff going on in the background between calls to your draw function (all of which affects the framerate) so I would recommend against relying on that in general.

这篇关于如何使gif运行一次并在Java中停止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆