简体   繁体   中英

Blinking tiled map Java Libgdx - rendering tiled maps

I have 3 tiles and i am trying to render them for a tile map. My Code So Far: public void

render(float delta) {

Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
game.batch.begin();
int w;
w = 120;

int h;
h = 1080/16;

int tileX = 0;
int tileY = 0;
Texture currentTile = null;
for(int i=0; i<w; i++){
    for(int j=0; j<h; j++){

        switch(MathUtils.random(2)){

        case 0:
            currentTile = grass;
            tiles.add(grass);
            break;
        case 1:
            currentTile = stone;
            break;
        case 2:
            currentTile = dirt;
            break;
            }
        game.batch.draw(currentTile, i*120, j*120);

    }
    }

The problem is hat it renders fine apart from the tiles keep blinking - as they are constantly being rerendered i think. But how can i stop this is the only way to use a 2d array, if so pls show how to:

Render the array , in this format what would i put instead of currentTile?:

game.batch.draw(currentTile, i*120, j*120);

Setting up the array , what should it look like, this?:

Texture[] tiles = new Texture[1];

How to add tiles to and construct the array properly , any other detail would be useful.

In my experience, a blinking screen occurs when areas of your canvas have nothing drawn to them. Before you start trying to draw the tiles, create/fill with some color (black, blue, whatever) that fills the screen. From here draw your BG items, tiles, player/entities, etc.

At each gameloop you are setting a new Random texture to the tile, this means that in 1 second a tile can change up to 60 times(fps) per second

To fix this, it depense whether the tiles should be able to change or not.

-if not, predefine them in the constructor

-if they can change, set a timed event and/or action to trigger it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM