我搜索了SpriteSheet的 API ,但我找不到任何关于如何制作具有不同大小精灵的精灵表的信息。
我正在使用的精灵表有一行 16x16px 的图块,它下面有一排 24x24px 的图块,下面是一排 8x8px 的图块,依此类推。
最初,我没有使用 Slick2D,而是使用 BufferedImage.getSubimage() 从精灵表的临时 BufferedImage 中获取每个精灵。我可以在这里使用类似的方法吗?
我搜索了SpriteSheet的 API ,但我找不到任何关于如何制作具有不同大小精灵的精灵表的信息。
我正在使用的精灵表有一行 16x16px 的图块,它下面有一排 24x24px 的图块,下面是一排 8x8px 的图块,依此类推。
最初,我没有使用 Slick2D,而是使用 BufferedImage.getSubimage() 从精灵表的临时 BufferedImage 中获取每个精灵。我可以在这里使用类似的方法吗?
我不相信在当前版本的 API 中可以直接创建子图像,至少在撰写本文时是这样。
但是,我可以想到三个可能的选项(除了您自己添加所述方法调用的选项 -毕竟它是开源的):
SpriteSheet
从同一个源实例化多个对象,每个 Sprite 大小一个对象。Image
Image
实例,并调用getSubImage
它以将其拆分Image
为三张图像,每种尺寸一张(24x24、16x16 等)。然后,从这些子图像中,实例化SpriteSheets
.您可以只保留一个 Image 并使用 Graphics 对象的 drawImage 方法的重载来指定在哪里绘制图像的哪一部分:
g.drawImage(image, x1, y1, x2, y2, srcX1, srcY1, srcX2, srcY2);
见 [javadoc](http://slick.cokeandcode.com/javadoc/org/newdawn/slick/Graphics.html#drawImage(org.newdawn.slick.Image, float, float, float, float, float, float, float , 漂浮))
第一个参数是图像的实例。接下来的两个参数定义屏幕上渲染开始的点。X2 和 y2 定义了渲染的终点。通常 x2 是 x1 + spriteWidth 并且 y2 是 y1 + spriteHeight,但是您可以更改这些值以绘制不同大小的精灵。最后四个参数的作用相同,但它们定义了将在屏幕上绘制的精灵表区域。
如果我们以您的示例为例,并且我们想从第三行绘制第二个图块,则调用将如下所示:
int tileWidth = 8;
int tileHeight = 8;
int sourceX = 40;
int sourceY = 8; //as its the sec
int drawX = 34;
int drawY = 65;
g.drawImage(image, drawX, drawY, drawX + tileWidth, drawY + tileHeight
, sourceX, sourceY, sourceX + tileWidth, sourceY + tileHeight);
When I work with spritesheet, I have hardcoded values in some (very rare cases, mostly tests) and a sprite class, that has the source x1, x2, y1 and y2 values stored. I can pack a bunch of them in a list or a map and like that I have a sprite index. Usually I generate the definitions somehow and then serialize the list, so I can simply reload that list, if I need it.
Here is a short example of my XML definition (I store the width and height rather then the x2 and y2 values in the xml, as I find it more human readable and more convenient for manual editing. After deserialization I calculate the x2 and y1 values):
<spriteSheet imageName="buildings" name="buildings">
<sprite name="1x2 industry 01" spriteX="0" spriteY="0" width="50" height="112"/>
<sprite name="1x2 quarters 01" spriteX="50" spriteY="0" width="50" height="112"/>
<sprite name="1x1 spaceport 01" spriteX="243" spriteY="112" width="51" height="56"/>
...
</spriteSheet>