简体   繁体   English

没有故障就无法在线程内加载tiledmap(libgdx)

[英]Can't load tiledmap within thread without it glitching(libgdx)

So if I have the code arranged like this(Inside a screen): 因此,如果我将代码安排成这样(在屏幕内):

private TiledMap m = new Tmxloader.load("map");
private OrthogonalTiledMapRenderer renderer = new OrthogonalTiledMapRenderer(m);

public void render(float delta) {

    renderer.setView(cam); 
    renderer.render();

}

It works perfectly fine. 它工作得很好。 The map is loaded, rendered, and there is no problem. 地图已加载,渲染,没有问题。

But when I put the loading of the map in a thread to reduce load time... 但是当我将地图的加载放在线程中以减少加载时间时...

private TiledMap m;
private OrthogonalTiledMapRenderer renderer;
private boolean loaded = false;

new Thread(new Runnable() {
        @Override
        public void run() {
            m = new Tmxloader.load("map");
            loaded = true;
        }
    }).start(); 

if(loaded){
    renderer = new OrthogonalTiledMapRenderer(m);
}

public void render(float delta) {

    renderer.setView(cam); 
    renderer.render();

}

The map renders incorrectly. 地图渲染不正确。 In fact, I wouldn't even call it the map rendering at this point. 实际上,这时我什至不称其为地图渲染。 All I see is some gray circles. 我只看到一些灰色圆圈。 I've tried everything and can't find a way around it. 我已经尝试了所有方法,但找不到解决方法。

(This isn't my excact code, you might say I don't need to load the map in a thread here, but in my game I do. Trust me.) (这不是我的确切代码,您可能会说我不需要在这里在线程中加载地图,但是在我的游戏中我可以。相信我。)

According to doc : 根据文档

In Libgdx only one thread (the thread that executes the app lifecycle callbacks) has a valid OpenGL context and can invoke OpenGL calls. 在Libgdx中,只有一个线程(执行应用程序生命周期回调的线程)具有有效的OpenGL上下文,并且可以调用OpenGL调用。 You can post a Runnable to the GDX thread from your other threads to get it to execute stuff on behalf of other threads. 您可以从其他线程向GDX线程发布Runnable,以使其能够代表其他线程执行任务。 Posted runnables will be executed before the next render callback is run. 已发布的可运行对象将在下一个渲染回调运行之前执行。 See Gdx.app.postRunnable() 参见Gdx.app.postRunnable()


If loading time is the issue then you should use AssetManager for your requirement and show loading screen when all your resource are loading. 如果加载时间是问题,则应使用AssetManager满足要求,并在加载所有资源时显示加载屏幕。

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

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