I’m trying to make a roguelike in java to practice. This is my code to generate a floor (right now just a big room with wall tiles on the edge). I’m trying to set certain tiles in my tile array to either a wall tile or floor tile. Although when they leave the setTile method, they revert back to their value before entering the method. I’m going insane. Here’s my code:
public Floor(int width, int height) {
this.tiles = new Tile[(width+1)*(height+1)];
this.width = width;
this.height = height;
generateTiles();
boolean test = false;
}
public Tile getTile(int x, int y)
{
return tiles[y * width + x];
}
public void setTile(int x, int y, Tile tile)
{
Tile tileToSet = getTile(x,y);
tileToSet = tile;
}
private void generateTiles() {
for (int i = 0; i < tiles.length; i++)
{
tiles[i] = new Tile();
}
//make the top wall
for (int i = 0; i<width;i++)
{
setTile(i,0,new WallTile());
}
}
}
Your
setTiledoesn’t make sense. You’re retrieving the tile which is currently at that position, storing it in a local variabletileToSetand then overwriting the value of that variable.What you’re trying to do is storing the given tile in the
tilesarray. Analogous to howgetTileis implemented, you can do this with:Note that this is not equivalent (but you seem to think it is) with: