繁体   English   中英

从Sprite表Java读取图像

[英]Reading images from a sprite sheet Java

我想在游戏中使用精灵表,并通过研究发现了这段代码。

    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 
        ); 
    } 
} 

我了解此代码片段如何将精灵表转换为数组,但是如何访问此数组。 只是sprites[i];

也可以将加载的精灵绑定到OpenGL纹理

int spritename = glgentextures;
{
sprites[i];
}

提前致谢。

要访问sheet.png中的特定图像,可以使用sprite [rowNum * cols + colNum]。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM