LibGDX:平铺地图的视差效果

时间:2017-11-15 21:26:15

标签: java android libgdx parallax tiled

需要在LibGDX和平铺地图的游戏中产生视差效果。背景应该以不同的速度移动然后前景。在文档https://github.com/libgdx/libgdx/wiki/Tile-maps中说:

  

分别渲染每个图层并修改每个图层   层,你也可以实现视差效果。

当前实施PlayScreenVariantOne所有功能都有效,但背景移动速度与前景相同

public class PlayScreenVariantOne implements Screen {

    private TextureAtlas atlas;
    private OrthographicCamera gameCam;
    private Viewport viewport;
    private TmxMapLoader mapLoader;
    private TiledMap map;
    private OrthogonalTiledMapRenderer renderer;
    ...

    // ...
    // ... bla-bla-bla...
    // ...
    @Override
    public void render(float delta) {
        update(delta);
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        renderer.setView(gameCam);
        renderer.render();

        game.batch.setProjectionMatrix(gameCam.combined);
        game.batch.begin();

        player.draw(game.batch);
        game.batch.end();
        hud.stage.draw();

    }
 }

视差实施:前景和背景以相同的速度渲染。PlayScreenVariantTwo前景。视差效果不起作用! :*(

public class PlayScreenVariantTwo implements Screen {

    private TextureAtlas atlas;
    private OrthographicCamera gameCam;
    private Viewport viewport;
    private TmxMapLoader mapLoader;
    private TiledMap map;
    private OrthogonalTiledMapRenderer renderer;
    ...

    // ...
    // ... bla-bla-bla...
    // ...
    @Override
    public void render(float delta) {
        update(delta);
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        int[] backgroundLayers = { 7 };
        int[] ground = { 9 };

        renderer.setView(gameCam.combined, (gameCam.position.x - gameCam.viewportWidth / 2 - 2), gameCam.position.y - gameCam.viewportHeight / 2, gameCam.viewportWidth, gameCam.viewportHeight);
        renderer.render(backgroundLayers);

        renderer.setView(gameCam.combined, (gameCam.position.x - gameCam.viewportWidth / 2) / 10, gameCam.position.y - gameCam.viewportHeight / 2, gameCam.viewportWidth  * 10, gameCam.viewportHeight);
        renderer.render(ground);

        game.batch.setProjectionMatrix(gameCam.combined);
        game.batch.begin();

        player.draw(game.batch);
        game.batch.end();

        hud.stage.draw();

    }
 }

1 个答案:

答案 0 :(得分:0)

您是否尝试简单地将X(或Y,您需要的任何东西)坐标与背景因子相乘:

a = ['a', 'b', 'c']
b = ['1', '2', '3']

c = []
for x, y in zip(a, b):
    c.append(x)
    c.append(y)

print (c)

['a', '1', 'b', '2', 'c', '3']

我添加了“* 0.5”,因此背景会慢两倍。不确定这两行中的哪一行会给你更好的结果 - 你必须尝试自己,但那就是ide - 乘以背景坐标,X或Y,你需要设置不同的速度。