我想在我的游戏中使用 Sprite 表,并且通过我所做的研究找到了这段代码。
BufferedImage bigImg = ImageIO.read(new File("sheet.png"));
// The above line throws an checked IOException which must be caught.
final int width = 10;
final int height = 10;
final int rows = 5;
final int cols = 5;
BufferedImage[] sprites = new BufferedImage[rows * cols];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
sprites[(i * cols) + j] = bigImg.getSubimage(
i * width,
j * height,
width,
height
);
}
}
我了解此代码片段如何将 Sprite 表转换为数组,但如何访问该数组。它只是sprites[i];
吗?
还可以使用
将加载的 Sprite 绑定(bind)到 OpenGL 纹理中int spritename = glgentextures;
{
sprites[i];
}
提前致谢。
最佳答案
要访问sheet.png中的特定图像,您可以使用sprite[rowNum*cols + colNum]。
关于java - 从 Sprite Sheet Java 读取图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10604824/