如何使用libgdx滚动地图?

时间:2014-08-14 19:59:05

标签: android libgdx

伙计们请帮帮我,好吧这是我的代码

private TiledMap map;
private OrthogonalTiledMapRenderer renderer;
private OrthographicCamera camera;

@Override
public void render(float delta) {
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    position.y = position.y - 4;


@Override
public void show() {

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

}

}

使用此代码,我在屏幕上看到地图,但我希望地图滚动,它应该停在屏幕的末尾,我想继续滚动到地图的末尾,我正在尝试从3天开始,请帮助我们:(

2 个答案:

答案 0 :(得分:0)

如果我的问题是正确的:你可以滚动但不会停止。

因此,如果您希望停止,则必须添加条件

public void render(float delta) {
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    position.y = position.y - 4;
    if(position.y < camera.viewportWidth - mapHeight)
    {
       position.y = camera.viewportWidth - mapHeight;
    }
}

答案 1 :(得分:0)

首先,您需要在地图上设置视图。 然后,您需要更改摄像机位置并更新摄像机。 如果你想在屏幕的末尾停下来 - 写下之前提到过的东西。

部分代码如下:

@Override
public void show() {
    ...
    camera = new OrthographicCamera();

    renderer = new OrthogonalTiledMapRenderer(map);        
    renderer.setView(camera);
    ...
}

public void render(float delta) {
    ...
    camera.position.y -= 4 * delta; //4 units per second
    if(camera.position.y < camera.viewportHeight/2 - mapHeight) {
       camera.position.y = camera.viewportHeight/2 - mapHeight;
    }
    camera.update();        
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    renderer.render();   
    ...
}