我正在尝试将相机设置为专注于中心的玩家,因为它会越过固定到位的 TiledMap。我正在为相机使用这个渲染代码:
OverworldWorld world;
Player player;
OrthographicCamera cam;
SpriteBatch batch;
TiledMap map;
TileAtlas tileAtlas;
TileMapRenderer tmr;
public Renderer(OverworldWorld world){
this.world = world;
world.setRenderer(this);
width = Gdx.graphics.getWidth();
height = Gdx.graphics.getHeight();
new TiledLoader();
map = TiledLoader.createMap(Gdx.files.internal("maps/island.tmx"));
tileAtlas = new TileAtlas(map, Gdx.files.internal("maps/"));
tmr = new TileMapRenderer(map, tileAtlas, 16, 16);
cam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
cam.setToOrtho(false, width, height);
cam.position.set(0, 0, 0);
cam.update();
batch = new SpriteBatch();
batch.setProjectionMatrix(cam.projection);
sr = new ShapeRenderer();
}
public void render(){
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
player = world.getPlayer();
cam.position.set(player.getPosition().x, player.getPosition().y, 0);
cam.update();
tmr.getProjectionMatrix().set(cam.combined);
Vector3 tmp = new Vector3();
tmp.set(0, 0, 0);
cam.unproject(tmp);
tmr.render((int) tmp.x, (int) tmp.y, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
结果是玩家被锁定在地图的左下角,移动玩家会使相机在地图上移动。运动是非常基本的:
player = world.getPlayer();
switch(keycode){
case Keys.W:
player.getVelocity().y = 1;
player.direction = "up";
break;
并使用此更新玩家位置(这是 0.9.8 版):
position.add(velocity.tmp().mul(Gdx.graphics.getDeltaTime() * speed));
是什么导致了这个问题,如何解决?