问题描述
我很难使用LibGdx将地图正确显示在我的android应用上.首先是我的代码:
I am having a hard time getting a map to display correctly on my android app using LibGdx. First up here is my code:
public class PlayState extends State {
private LittleMan littleMan;
private TiledMap map;
OrthogonalTiledMapRenderer tiledMapRenderer;
OrthographicCamera camera;
public PlayState(GameStateManager gsm) {
super(gsm);
littleMan = new LittleMan(50,100);
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
camera = new OrthographicCamera();
camera.setToOrtho(false, w,h);
camera.update();
TmxMapLoader loader = new TmxMapLoader();
map = loader.load("ste_barbe_map.tmx");
tiledMapRenderer = new OrthogonalTiledMapRenderer(map);
}
@Override
protected void handleInput() {
}
@Override
public void update(float dt) {
}
@Override
public void render(SpriteBatch sb) {
// if viewport modified
// sb.setProjectionMatrix(cam.combined);
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
tiledMapRenderer.setView(camera);
tiledMapRenderer.render();
/*
sb.begin();
sb.draw(littleMan.getTexture(),littleMan.getPosition().x,littleMan.getPosition().y);
sb.end();
*/
}
@Override
// free some ressources
public void dispose() {
}
}
因此,当我尝试运行该应用程序并调用render方法时,我只会得到一个红色背景,上面没有任何内容.这是我尝试过的一些事情:
So when i try to run the app and call the render method i just get a red background with nothing on it.Here are some things i tried:
- 使用仅带有一行图块的较小地图,即可正确显示(即使我不确定其位置是否正确):
- 按比例缩放:我可以显示地图,但是它很小,或者我只能看到边缘
此行更改为:tiledMapRenderer = new OrthogonalTiledMapRenderer(map,0.5f);
我得到:
with this line changed to : tiledMapRenderer = new OrthogonalTiledMapRenderer(map,0.5f);
I get:
此行:tiledMapRenderer = new OrthogonalTiledMapRenderer(map,1/8f);
我得到了这张非常缩小的地图图像:
I get this very zoomed out image of the map:
有人知道我在做什么错吗?也许我的tmx文件不好,也许是相机的东西,或者我真的不知道的东西.感谢您的帮助!
Does anyone know what i am doing wrong? Maybe my tmx file is bad, maybe it's something with the camera, or something else i really don't know.Thanks for the help!
推荐答案
根据这样的地图设置Camera ViewPort.
Set Camera ViewPort according to your map like this.
TmxMapLoader loader = new TmxMapLoader();
map = loader.load("ste_barbe_map.tmx");
tiledMapRenderer = new OrthogonalTiledMapRenderer(map);
TiledMapTileLayer layer=(TiledMapTileLayer)map.getLayers().get(0);
float w=layer.getTileWidth()*layer.getWidth(); // 100*34
float h=layer.getTileHeight()*layer.getHeight(); // 100*34
camera=new OrthographicCamera(w,h);
// you can manually set camera viewport
//camera=new OrthographicCamera(100*34,100*34);
您也可以尝试
camera=new OrthographicCamera(34,34);
float unitScale = 1 / 100f;
OrthogonalTiledMapRenderer renderer = new OrthogonalTiledMapRenderer(map, unitScale);
了解更多unitScale的工作原理:
https://github.com/libgdx/libgdx/wiki/Tile-maps#rendering-tiled-maps
Get more how unitScale works :
https://github.com/libgdx/libgdx/wiki/Tile-maps#rendering-tiled-maps
编辑
我检查了.tmx文件,它有3层.在索引0处有一个TiledMapImageLayer
,这就是为什么它会给您带来转换错误.在索引1和2处有TiledMapTileLayer
,因此从索引1获得层.
I checked .tmx file, it has 3 layer. There is a TiledMapImageLayer
at 0 index that's why it give you cast error. At index 1 and 2 there is TiledMapTileLayer
so get layer from index 1.
我尝试将您的.tmx加载到我的代码中,并在显示时呈现.
I tried to load your .tmx in my code and it render on display.
public class TestGame extends Game {
TiledMap map;
OrthographicCamera camera;
TiledMapRenderer tiledMapRenderer;
@Override
public void create() {
TmxMapLoader tmxMapLoader=new TmxMapLoader();
map=tmxMapLoader.load("ste_barbe_map.tmx");
TiledMapTileLayer layer=(TiledMapTileLayer) map.getLayers().get(1);
tiledMapRenderer= new OrthogonalTiledMapRenderer(map);
camera=new OrthographicCamera();
camera.update();
float widthHeightRatio=1.66f;
camera.setToOrtho(false,(layer.getWidth()*layer.getTileWidth())/widthHeightRatio,layer.getHeight()*layer.getTileHeight());
}
@Override
public void render() {
Gdx.gl.glClearColor(1,0,0,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
tiledMapRenderer.setView(camera);
tiledMapRenderer.render();
}
}
在水平和垂直方向具有相同图块数量的地图.您的游戏将是横向的还是纵向的,因此请保持这样的比例(横向时水平为25瓦片,垂直为15瓦片).
Map having same number of tile in horizontal and in vertical. Your game will be either in landscape or portrait so keep a ratio for this like(for landscape 25 tile in horizontal and 15 tile in vertical).
widthHeightRatio
变为1.
这篇关于无法使用OrthogonalTiledMapRenderer(Android)渲染.tmx地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!