0
\$\begingroup\$

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:

Supposed to be 6 tiles wide and 2 tiles high.

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – DMGregory Jan 22 '17 at 10:38
  • \$\begingroup\$ 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. \$\endgroup\$ – Ammar Tarajia Jan 22 '17 at 12:03
0
\$\begingroup\$

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.

|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.