LibGDX - 调用 TiledMapRenderer.render() 时应用程序崩溃 [英] LibGDX - Application crashes when call TiledMapRenderer.render()

查看:21
本文介绍了LibGDX - 调用 TiledMapRenderer.render() 时应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@Override
public void render(float delta) {
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    camera.update();
    sprite.setProjectionMatrix(camera.combined);

    mLevel.getTiledMapRenderer().getProjectionMatrix().set(camera.combined);
    Vector3 tmp = new Vector3();
    tmp.set(0, 0, 0);
    camera.unproject(tmp);
    mLevel.getTiledMapRenderer().render(tmp.x, tmp.y, camera.viewportWidth, camera.viewportHeight);

    sprite.begin();
    ...
    sprite.end();
}

这是我在启动桌面版时得到的:

Here is what I get when launching the desktop version :

Exception in thread "LWJGL Application" java.lang.IllegalArgumentException: Number of remaining buffer elements is 0, must be at least 1. Because at most 1 elements can be returned, a buffer with at least 1 elements is required, regardless of actual returned element count
    at org.lwjgl.BufferChecks.throwBufferSizeException(BufferChecks.java:162)
    at org.lwjgl.BufferChecks.checkBufferSize(BufferChecks.java:189)
    at org.lwjgl.BufferChecks.checkBuffer(BufferChecks.java:230)
    at org.lwjgl.opengl.GL15.glBufferData(GL15.java:141)
    at com.badlogic.gdx.backends.lwjgl.LwjglGL20.glBufferData(LwjglGL20.java:93)
    at com.badlogic.gdx.graphics.glutils.VertexBufferObject.bind(VertexBufferObject.java:208)
    at com.badlogic.gdx.graphics.Mesh.bind(Mesh.java:268)
    at com.badlogic.gdx.graphics.g2d.SpriteCache.begin(SpriteCache.java:868)
    at com.badlogic.gdx.graphics.g2d.tiled.TileMapRenderer.render(TileMapRenderer.java:336)
    at com.badlogic.gdx.graphics.g2d.tiled.TileMapRenderer.render(TileMapRenderer.java:286)
    at com.crunsh.libgdx.screens.GameScreen.render(GameScreen.java:102)
    at com.badlogic.gdx.Game.render(Game.java:46)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:202)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:131)

我已经尝试了这个教程这个,并且我在同一行 mLevel.getTiledMapRenderer().render(tmp.x, tmp.y, camera.viewportWidth, camera.viewportHeight); .

I've tried both this tuto and this one, and I get the same error at the same line mLevel.getTiledMapRenderer().render(tmp.x, tmp.y, camera.viewportWidth, camera.viewportHeight);.

我刚刚注意到我的 .tmx 文件的数据是由平铺地图编辑器自动编码的,所以我只是将其停用,但仍然出现相同的错误.
如果我复制/粘贴 这个项目(我的意思是整个项目,不仅仅是 render() 方法)它可以工作,但是当我尝试在我自己的项目中加载我自己的地图时它只是崩溃了......

EDIT : I just noticed that the data of my .tmx file was automatically encoded by Tiled Map Editor, so I just desactivated it, but still get the same error.
If I copy/paste this project (I mean the whole project, not only the render() method) it works but when I try to load my own map in my own project it just crashes ...

所以之后我决定简化我的 render() 方法,就像我之前给出的链接一样,并使用作者提供的 TiledMapHelper 类,所以有现在是我的 render() 方法:

So after that I decided to simplify my render() method by just doing like the link I gave before and by using the TiledMapHelper class the author provide, so there is my render() method now:

mHelper.getCamera().update();
mHelper.render();

仍然崩溃iiiiiiiiiiiiiiiingg.....
如果有人可以提供一些帮助,我们将不胜感激,因为 atm 我真的快疯了!

Still crashiiiiiiiiiiiiiingg.....
If someone can please provide some help it would be greatly appreciated because atm i'm really going mad !

推荐答案

我找到了导致错误的原因:
1)我的packfile"中的所有图块的索引都是-1
2) 我的packfile"中的所有图块的名称与其原始图块文件名相同,但与包含所有图块打包的 .png 文件的名称不同.

I found what caused the error :
1) all the tiles in my "packfile" had -1 as their index
2) all the tiles in my "packfile" had the same name as their original tile file name, but not the same name as the .png file containing all the tiles packed.

目前我的屏幕是黑色的,所以我认为没有绘制图块(可能是因为我为包文件中的图块提供了随机索引 - 已修复,请参阅帖子末尾)但没有抛出异常这就是重点.

Currently my screen is black so I think no tile is draw (probably because I gave random indexes to the tiles in the packfile - fixed, see the end of the post) but no exceptions are thrown and that's the point.

这些错误是由于我用来打包应该让我赢得时间的程序,哈哈.所以我会尝试不同的选择或使用其他东西.
如果你想知道程序是TexturePacker GUI"v3.1.0.

Those mistakes are due to the program I used to pack the tiles that was supposed to make me win time lol. So I will try different options or use something else.
If you want to know the program was "TexturePacker GUI" v3.1.0.

如果您知道我应该/不应该使用哪些选项来避免错误,请告诉我.

If you know what options I should/not use to avoid errors please let me know.

找到了一个有用的工具来打包瓷砖,这里是:http://freigabe.philweb.de/bubblr/texturepacker_edited.jar(链接已失效;改用这个 => http://bit.ly/1a831nv 或其他打包工具 => http://bit.ly/1aLgAFt)

EDIT : Found a usefull and working tool for packing tiles, here it is : http://freigabe.philweb.de/bubblr/texturepacker_edited.jar (link is dead ; use this one instead => http://bit.ly/1a831nv or another packing tool => http://bit.ly/1aLgAFt)

要使用它,请打开 cmd 并转到下载 .jar 文件的目录,然后:

To use it open cmd and go to the directory where your downloaded the .jar file and then :

//You should create, in the same directory of the .jar file, two additionnal folders
//Call the first folder 'input' and put in all your tiles (rename them "level_1.png", "level_..")
//Call the 2nd folder 'output' and leave it empty
//Then just type in the command prompt : java -jar nameOfDownloadedJarFile.jar input output level
//Then rename the "input1.png" into "level.png", and "level.pack" into "level packfile"
//Open "level packfile" with notepad and change "input1.png" into "level.png"
//Then draw your map using "level.png" in TiledMapEditor
//Then just follow dpk' tutorial for rendering the map and it should work

这是我提到的 dpk 教程,只需从这里开始:http://dpk.net/2011/05/01/libgdx-box2d-tiled-maps-full-working-example-part-1/#p4

Here is dpk's tutorial I mentioned, just follow from here : http://dpk.net/2011/05/01/libgdx-box2d-tiled-maps-full-working-example-part-1/#p4

如果应用程序没有崩溃但屏幕是黑色的,只需增加相机视口的宽度&高度.

If application doesn't crashes but the screen is black, just increase camera's viewport's width & height.

快乐映射:) !

这篇关于LibGDX - 调用 TiledMapRenderer.render() 时应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆