gpt4 book ai didi

Java Slick2d - 从 spritesheet 加载图 block 抛出异常

转载 作者:行者123 更新时间:2023-12-01 15:26:24 26 4
gpt4 key购买 nike

block 类

public class Block {

public enum BlockType {
Dirt,
Grass,
Selection
}

BlockType Type;
Vector2f Position;
Image texture;
boolean breakable;

public Block(BlockType Type, Vector2f Position, Image texture, boolean breakable) {
this.Type = Type;
this.Position = Position;
this.texture = texture;
this.breakable = breakable;
}

public BlockType getType() {
return Type;
}
public void setType(BlockType value) {
Type = value;
}

public Vector2f getPosition() {
return Position;
}
public void setPosition(Vector2f value) {
Position = value;
}

public Image gettexture() {
return texture;
}
public void settexture(Image value) {
texture = value;
}

public boolean getbreakable() {
return breakable;
}
public void setbreakable(boolean value) {
breakable = value;
}

}

图 block 生成类

public class TileGen {

Block block;
public Block[] tiles = new Block[3];
public int width, height;
public int[][] index;
boolean selected;
int mouseX, mouseY;
int tileX, tileY;

Image dirt, grass, selection;
SpriteSheet tileSheet;

public void init() throws SlickException {
tileSheet = new SpriteSheet("assets/tiles/tileSheet.png", 64, 64, new Color(0,0,0));

grass = tileSheet.getSprite(0,0);
dirt = tileSheet.getSprite(64,0);
selection = tileSheet.getSprite(128,0);

tiles[0] = new Block(BlockType.Grass, new Vector2f(tileX,tileY), grass, true);
tiles[1] = new Block(BlockType.Dirt, new Vector2f(tileX,tileY), dirt, true);

width = 50;
height = 50;

index = new int[width][height];

Random rand = new Random();
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
index[x][y] = rand.nextInt(2);
}
}
}

public void update(GameContainer gc) {
Input input = gc.getInput();
mouseX = input.getMouseX();
mouseY = input.getMouseY();
tileX = mouseX / width;
tileY = mouseY / height;

if(input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
selected = true;
}
else{
selected = false;
}
System.out.println(tileX);
}

public void render() {
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
tiles[index[x][y]].texture.draw(x * 64, y *64);

if(IsMouseInsideTile(x, y))
selection.draw(x * 64, y * 64);
if(selected && tiles[index[x][y]].breakable) {
if(tiles[index[tileX][tileY]].texture == grass)
tiles[index[tileX][tileY]].texture = dirt;
}
}
}
}

public boolean IsMouseInsideTile(int x, int y)
{
return (mouseX >= x * 64 && mouseX <= (x + 1) * 64 &&
mouseY >= y * 64 && mouseY <= (y + 1) * 64);
}

我不确定我做错了什么,我是 slick2d 的新手。当我尝试从 Sprite 表初始化图 block 时,它会抛出异常。我的tileGen 类中的init 就是问题所在。

Exception in thread "main" java.lang.RuntimeException: SubImage out of sheet bounds: 64,0
at org.newdawn.slick.SpriteSheet.getSprite(SpriteSheet.java:208)
at com.synyst3r1.game.TileGen.init(TileGen.java:32)
at com.synyst3r1.game.Game.init(Game.java:23)
at org.newdawn.slick.AppGameContainer.setup(AppGameContainer.java:390)
at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:314)
at com.synyst3r1.game.Game.main(Game.java:50)

最佳答案

getSprite() 中的 (x, y)是单元位置,而不是像素位置。因此,您需要 getSprite(1, 0)getSprite(2, 0)(假设您的图像为 192x64)。

关于Java Slick2d - 从 spritesheet 加载图 block 抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10079782/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com