简体   繁体   English

闪烁平铺的地图Java Libgdx-渲染平铺的地图

[英]Blinking tiled map Java Libgdx - rendering tiled maps

I have 3 tiles and i am trying to render them for a tile map. 我有3个图块,我正在尝试将它们渲染为图块地图。 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: 但是我如何才能停止这是使用2d数组的唯一方法,如果可以,请显示如何:

Render the array , in this format what would i put instead of currentTile?: 以这种格式渲染数组 ,我将放置什么而不是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. 从这里绘制您的BG物品,图块,玩家/实体等。

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 在每个游戏循环中,您要为图块设置一个新的“随机”纹理,这意味着一秒钟内,图块最多可以每秒变化60次(fps)

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 -如果可以更改,请设置一个定时事件和/或操作来触发它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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