0
votes

I want to create a random tiled map for my game, this is what I have so far:

switch(MathUtils.random(2)){

        case 0:
            tileX+=16;
            loadedTiles ++;
            //game.batch.draw(tile1, tileX, tileY);
            System.out.print("tile1");
            currentTile = tile1;
            break;
        case 1:
            tileX+=16;
            loadedTiles ++;
            //game.batch.draw(tile2, tileX, tileY);
            System.out.print("tile2");
            currentTile = tile2;
            break;
        case 2:
            tileX+=16;
            loadedTiles ++;
            //game.batch.draw(tile3, tileX, tileY);
            System.out.print("tile3");
            currentTile = tile3;
            break;
            }

        game.batch.begin();
        game.batch.draw(currentTile, tileX, tileY);
        game.batch.end();
        }

Instead of rendering them each individually i would like to add them to an array and render them all together, so if i have an array such as this:

ArrayList<Texture> tiles;

Then add something to all of he case options like:

tiles.add(tile1);

Problem:

How do i render the array and get the relevent co-ordinates for them to be rendered, does this get added to the array?

2
use math to calculate the coordinates of the point of where the bottom left corner (unless you inverted the y axis) of the tile should beEpicPandaForce
@Zhuinden Is there an example of this and how would you render the array?user3165683
Just a comment, tileX+=16; loadedTiles++;is always executed, so you could put that outside the switch statement to avoid duplicated code.Simon
@Simon Thank you i just changed that, i'm always open to improvementsuser3165683
Technically I'm not even sure why you're trying to make a List of Texture when what you basically want to do is have one global texture, select the required TextureRegion for the Tiles, create a Sprite for each tile you want to display while as Sprite is not a serializable class or at least it should be transient as it has Texture on its object graph ( github.com/libgdx/libgdx/wiki/Saved-game-serialization ), so you're supposed to have a class that shares the resource only for rendering the object. If I understand your problem properly, anyways.EpicPandaForce

2 Answers

0
votes

That would be using a batcher, from these lines here

   game.batch.begin();
   game.batch.draw(currentTile, tileX, tileY);
   game.batch.end();

It appears you're already using one. Place the begin to before the loop where you draw the tiles is and the end to where the loop ends.

0
votes

I solved a similar problem like this:

batch.begin();
for (Ground ground : groundArray){
    batch.draw(ground.getTextureRegion(), ground.x, ground.y);
}
batch.end();

For more info look HERE

If you have some questions i will be happy to update my answer, just post in comments.

There you go:

public class Ground {
public float x;
float y;
private TextureRegion texture;
private Rectangle bounds;

public Ground(float x, float y){
    texture = Assets.atlas.findRegion("ground");
    this.x = x;
    this.y = y;
    bounds = new Rectangle(x, y, 100, 498);
}

public TextureRegion getTextureRegion(){
    return texture;
}

public Rectangle getBounds(){
    return bounds;
}
}

If you want to know about Asstes.atlas.findRegions look HERE