简体   繁体   English

停止移动相机

[英]Stop Camera from moving

I've been trying to stop moving my camera at the end of my map, but it continues to move. 我一直试图在地图末端停止移动相机,但相机仍在移动。

Here is my code: 这是我的代码:

@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();

}
}

I couldn't find any error in my code. 我在代码中找不到任何错误。

So my question here is, how to stop the camera from moving at the end of the map? 因此,我的问题是,如何停止相机在地图末端移动?

You have at least one layer in the tiled map, so: 平铺地图中至少有一层,因此:

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();

And now, you know the layer's width in pixels , and yo can make sure that camera.position.x+Gdx.graphics.getDeltaTime()*200; 现在,您知道了该图层的宽度( 以像素为单位) ,您可以确保camera.position.x+Gdx.graphics.getDeltaTime()*200; never exceeds this length. 永远不要超过这个长度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM