0

Block Class

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

}

Tile Generation Class

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

I am not sure what I am doing wrong, I am new to slick2d. When I try to init my tiles from the spritesheet it throws an exception. The init in my tileGen class is where the problem is.

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)
Corey
  • 73
  • 4
  • 13

1 Answers1

1

The (x, y) in getSprite() are the cell position, not the pixel position. So, you want getSprite(1, 0) and getSprite(2, 0) (assuming your image is 192x64).

Russell Zahniser
  • 16,188
  • 39
  • 30
  • I actually just figured this out seconds before you posted :). Thanks for the help tho. I'm going to accept your answer – Corey Apr 09 '12 at 22:52
  • One other question though. How do you make colors transparent? I have a black grid and I don't want it to draw it is only there to line up the tiles – Corey Apr 09 '12 at 22:57
  • When creating sprite sheets in GIMP, I normally just turn on a 64x64 grid so that it will be visible while editing, but not saved in the file. If you really need to have the grid in the file, you could load the file into a `BufferedImage` with `ImageIO`, iterate through looking for 0xFF000000 pixels and setting them to 0, and then create the sprite sheet from the modified image. – Russell Zahniser Apr 09 '12 at 23:02
  • Okay thanks I will have to download GIMP. I am using a plugin on paint.net and it actually draws the grid. I don't need them in the file at all so I will use GIMP – Corey Apr 09 '12 at 23:05