Map Engine Rendering Broken

So I've been making my own tile map engine in LibGDX as the one currently implemented doesn't support collision detection with scaling. So I've come up with my own Layer and Map class and a drawing function for the map. Currently this is what it produces:

Here's the rendering code:

public void draw(SpriteBatch batch) {
for(Layer layer : layers) {
for(int x = 0; x < layer.getTileMap().length; x++) {
for(int y = 0; y < layer.getTileMap()[x].length; y++) {
batch.draw(layer.getTiles()[layer.getTileMap()[x][y]],
xOffset + (x * ((layer.getTileWidth() * scale))), yOffset + (y * ((layer.getTileHeight() * scale))),
(layer.getTileWidth() * scale), (layer.getTileHeight() * scale));
}
}
}
}


EDIT: Yes, I'm aware the map looks weird, it's just random textures thrown in from a tile-sheet.

• What would your expected output look like in this case? It's tough to know what's wrong with the output you've shown if we don't know what you were trying to do instead, or how it differs. – DMGregory Jan 22 '17 at 10:38
• Good point, it's meant to look like the bottom row. The answer basically came to me this morning as I realised that the x and y need to be switched in order for it to function correctly as the x represents the columns rather than the rows. – Ammar Tarajia Jan 22 '17 at 12:03

Here's the new rendering code:

public void draw(SpriteBatch batch) {
for(Layer layer : layers) {
for(int y = 0; y < layer.getTileMap().length; y++) {
for(int x = 0; x < layer.getTileMap()[y].length; x++) {
batch.draw(layer.getTiles()[layer.getTileMap()[x][y]],
xOffset + (x * ((layer.getTileWidth() * scale))), yOffset + (y * ((layer.getTileHeight() * scale))),
(layer.getTileWidth() * scale), (layer.getTileHeight() * scale));
}
}
}
}


The x and y simply needed to be switched.