java游戏启动gif,如何使gif运行一次并在Java中停止?

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;

}

解决方案

The usual solution to this is to not use a 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() 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值