0

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?

user3165683
  • 347
  • 1
  • 9
  • 28
  • 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 be – EpicPandaForce Aug 19 '14 at 11:19
  • @Zhuinden Is there an example of this and how would you render the array? – user3165683 Aug 19 '14 at 11:21
  • Just a comment, `tileX+=16; loadedTiles++;`is always executed, so you could put that outside the switch statement to avoid duplicated code. – Simon Aug 19 '14 at 11:22
  • @Simon Thank you i just changed that, i'm always open to improvements – user3165683 Aug 19 '14 at 11:24
  • 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 ( https://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 Aug 19 '14 at 11:26
  • @Zhuinden Is having sprites for each tile not inefficient for a tiled map? I wanted to write all of the tiles to an array then render the array to load the tiles in the correct order? – user3165683 Aug 19 '14 at 11:30
  • Hmmm.. I actually haven't used `Tile`. To be honest, now I'm not sure if I should have multiple Sprites or 1 sprite for each object type that is set to places then rendered. I will look into it. – EpicPandaForce Aug 19 '14 at 11:45

2 Answers2

0

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.

Joshua Waring
  • 619
  • 7
  • 23
  • Does this have to be in the render method and where would you put your clear screen? – user3165683 Aug 19 '14 at 11:34
  • because when i run it i just get a black screen and i cant see the tiles – user3165683 Aug 19 '14 at 11:38
  • All .draw calls should be between .begin and .end in the documentation https://github.com/libgdx/libgdx/wiki/Spritebatch%2C-Textureregions%2C-and-Sprites They open it just after the clear and end it before the render function ends. – Joshua Waring Aug 19 '14 at 11:40
0

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

Community
  • 1
  • 1
lxknvlk
  • 2,744
  • 1
  • 27
  • 32