对于我正在制作的游戏,我使用Tiled来制作和编辑地图。但是,这些地图不会加载到我目前所做的样本中:
package com.bluezamx.magillion.screens;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.maps.MapObject;
import com.badlogic.gdx.maps.objects.RectangleMapObject;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.viewport.FillViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.bluezamx.magillion.Magillion;
import com.bluezamx.magillion.utils.Constants;
public class WorldScreen implements Screen {
private Magillion game;
private OrthographicCamera gameCam;
private Viewport gamePort;
private Stage stage;
//Tiled variables
private TmxMapLoader mapLoader;
private TiledMap map;
private OrthogonalTiledMapRenderer maprenderer;
// Box2D variables
private World world;
private Box2DDebugRenderer debug;
public WorldScreen (Magillion game) {
this.game = game;
gameCam = new OrthographicCamera();
mapLoader = new TmxMapLoader();
map = mapLoader.load("TestWorld.tmx");
maprenderer = new OrthogonalTiledMapRenderer(map, 1 / Constants.PPM);
gamePort = new FillViewport(Constants.WIDTH / Constants.PPM, Constants.HEIGHT / Constants.PPM, gameCam);
gameCam.position.set((gamePort.getWorldWidth() / 2), (gamePort.getWorldHeight() / 2), 0);
world = new World(new Vector2(0,0), true);
debug = new Box2DDebugRenderer();
debug.SHAPE_STATIC.set(1, 0, 0, 1);
for(MapObject object : map.getLayers().get(1).getObjects().getByType(RectangleMapObject.class)) {
Rectangle rect = ((RectangleMapObject) object).getRectangle();
bdef.type = BodyDef.BodyType.StaticBody;
bdef.position.set(rect.getX() + rect.getWidth() / 2, rect.getY() + rect.getHeight() / 2);
body = world.createBody(bdef);
shape.setAsBox(rect.getWidth() / 2, rect.getHeight() / 2);
fdef.shape = shape;
body.createFixture(fdef);
}
}
@Override
public void show() {}
private void update(float dt) {
handleInput(dt);
world.step(1/60f, 6, 2);
player.update(dt);
gameCam.update();
maprenderer.setView(gameCam);
}
@Override
public void render(float dt) {
update(dt);
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
debug.render(world, gameCam.combined);
game.batch.setProjectionMatrix(gameCam.combined);
maprenderer.render();
game.batch.begin();
player.draw(game.batch);
game.batch.end();
}
@Override
public void resize(int width, int height) {}
@Override
public void pause() {}
@Override
public void resume() {}
@Override
public void hide() {}
@Override
public void dispose() {
map.dispose();
maprenderer.dispose();
world.dispose();
debug.dispose();
}
}
当我运行我的文件(自动将游戏屏幕设置为此文件)时,我只得到一个黑屏。地图本身没有显示。地图是640 * 480大,放在标准的android / assets文件夹中。
我尝试在网上找到的其他代码中运行我的地图并在那里工作。然而,我无法弄清楚我的错误。
答案 0 :(得分:1)
您需要使用设备屏幕宽度和高度更新视口。
@Override
public void resize(int width, int height) {
// use true here to center the camera
gamePort.update(width,height,false);
}
看看this回答,最近我添加了解决方案。
答案 1 :(得分:0)
看起来您的地图实际上已渲染,但只是您的视口位于地图位置之外。 尝试使用:
gamecam.setToOrtho(false, gamePort.getWorldWidth()/2, gamePort.getWorldHeight()/2);
而不是
gameCam.position.set((gamePort.getWorldWidth() / 2), (gamePort.getWorldHeight() / 2), 0);