Libgdx滚动TiledMaps

时间:2014-08-12 12:14:33

标签: java android libgdx

我试图让我的libgdx地图滚动,但我不知道使地图滚动的代码,请大家帮助我,我想地图滚动像飞扬的鸟类游戏,这是我的我的代码在屏幕上显示地图

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

    if(Gdx.input.isTouched()){
        position.y = position.y +10;
    }

    if(position.y < 0){
        position.y = 0;
    }

    if(position.y > Gdx.graphics.getHeight() -70){
        position.y = Gdx.graphics.getHeight() - 70;
    }

    renderer.setView(camera);
    renderer.render();

//tells the computer when to start drawing textures
    batch.begin();
    batch.draw(Green1, position.x, position.y, 50, 50);
    batch.end();

}

@Override
public void show() {
    Green1 = new Texture("img/Green1.png");
    batch = new SpriteBatch();
    position = new Vector2(20, Gdx.graphics.getHeight());

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

    renderer = new OrthogonalTiledMapRenderer(map);

    camera = new OrthographicCamera();
}

@Override
public void hide() {


}

@Override
public void create() {
    Green1 = new Texture("img/Green.png");
    batch = new SpriteBatch();
    position = new Vector2(20, Gdx.graphics.getHeight());

}

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

当我学习构建2D游戏时,我使用星际攻击作为例子。你可以在这里找到他们的github存储库:https://github.com/chrismorales/2d-sidescroller&amp;&amp; https://github.com/chrismorales/2d-sidescroller/blob/master/star-assault/src/net/obviam/starassault/view/WorldRenderer.java应该是有用的。