停止相机移动

时间:2014-09-07 16:25:55

标签: java android libgdx android-camera android-maps

我一直试图在地图的末尾停止移动相机,但它继续移动。

这是我的代码:

@Override
public void render(float delta) {       
//moving tiled map
camera.position.x=camera.position.x+Gdx.graphics.getDeltaTime()*200;

camera.update();
//...........................................

Gdx.gl.glClearColor(1, 0, 0, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);


// renderer camera and map
renderer.setView(camera);
renderer.render();
//...................................................

  }

   @Override
   public void show() {

   batch = new SpriteBatch();


   map = new TmxMapLoader().load("maps/map1.tmx");

   renderer = new OrthogonalTiledMapRenderer(map);

   camera = new OrthographicCamera();
   }

   @Override
   public void hide() {

   }

   @Override
   public void create() {

   }

  @Override
  public void resize(int width, int height) {
  camera.viewportWidth = width;
  camera.viewportHeight = height;
  camera.position.set(width/2f, height/3f, 0); //by default camera position on (0,0,0)
  camera.update();


  }

  @Override
  public void render() {



  }

 @Override
 public void pause() {


 }

@Override
public void resume() {


}

@Override
public void dispose() {
map.dispose();
renderer.dispose();

}
}

我的代码中找不到任何错误。

所以我的问题是,如何阻止相机在地图末端移动?

1 个答案:

答案 0 :(得分:0)

平铺地图中至少有一个图层,所以:

TiledMap map = ......//whatever method that you are using to load a map
//skip to the part that the map is loaded
TiledMapLayer layer = (TiledMapLayer)map.getLayers().get(0);
int layerWidth = layer.getWidth()*layer.getTileWidth();

现在,您知道图层的宽度以像素为单位,并且您可以确保camera.position.x+Gdx.graphics.getDeltaTime()*200;永远不会超过此长度。