我正在制作一款 2D 平台游戏,当某件事发生时我想要一个爆炸动画。我正在使用 lwjgl 加载纹理,但是当我尝试加载 gif 时,它只显示第一帧。那么有没有办法告诉它显示下一帧,或者有没有特殊的方法来加载它?
最佳答案
以下是如何使用 Java 和 LWJGL 实现您所描述的步骤的总体思路:
使用动画 GIF 文件来表示爆炸动画。
使用 GIFDecoder 等库读取 GIF 帧并将其解码为单独的图像。
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
// Use GIFDecoder to read and decode the frames of the GIF
import gifdecoder.GIFDecoder;
// Create a GIFDecoder object to read the animation file
GIFDecoder decoder = new GIFDecoder();
decoder.read("explode.gif");
// Get the number of frames in the animation
int frameCount = decoder.getFrameCount();
// Create an array to store the frames of the animation
BufferedImage[] frames = new BufferedImage[frameCount];
// Decode the frames of the animation and store them in the array
for (int i = 0; i < frameCount; i++) {
frames[i] = decoder.getFrame(i);
}
使用LWJGL的TextureLoader将动画的每一帧作为单独的纹理加载。
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
// Create an array to store the textures of the animation
Texture[] textures = new Texture[frameCount];
// Load each frame of the animation as a separate texture
for (int i = 0; i < frameCount; i++) {
textures[i] = TextureLoader.getTexture("PNG", new ByteArrayInputStream(ImageIO.read(frames[i])));
}
使用LWJGL的渲染函数在适当的位置绘制动画的当前帧
textures[currentFrame].bind();
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0, 0);
GL11.glVertex2f(x, y);
GL11.glTexCoord2f(1, 0);
GL11.glVertex2f(x + width, y);
GL11.glTexCoord2f(1, 1);
GL11.glVertex2f(x + width, y + height);
GL11.glTexCoord2f
关于java - (LWJGL) 如何加载动画GIFS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29308314/