【问题标题】:Reading images from a sprite sheet Java从精灵表 Java 中读取图像
【发布时间】:2012-05-16 00:27:25
【问题描述】:

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

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

我了解这个 sn-p 将如何将精灵表转换为数组,但是我如何访问这个数组。只是sprites[i]; 吗?

也可以使用

将加载的精灵绑定到 OpenGL 纹理中
int spritename = glgentextures;
{
sprites[i];
}

提前致谢。

【问题讨论】:

    标签: java lwjgl


    【解决方案1】:

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

    【讨论】:

    • 谢谢,我可以在 glgentextures 中使用这个调用吗?
    • 好的,我尝试了多种方法,但无法让它工作,任何人都有一些我可以阅读并学习如何正确操作的代码,所以我知道如何做。跨度>
    • 你试过TextureIO.newTexture(sprite[i], false)吗?此外,请注意精灵尺寸,因为此代码仅适用于分割成 25 个 10x10 像素图像的图像。
    猜你喜欢
    • 2022-12-05
    • 1970-01-01
    • 2015-08-23
    • 2013-11-29
    • 1970-01-01
    • 2015-08-10
    • 2012-02-10
    • 1970-01-01
    • 2014-02-03
    相关资源
    最近更新 更多