Rendering 2D array Libgdx Java

喜你入骨 提交于 2019-12-12 01:44:22

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/25382002/rendering-2d-array-libgdx-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!