如何使用LibGDX实现水平滚动?

时间:2019-03-10 19:55:49

标签: java android libgdx horizontal-scrolling

我想编写一个类似于旧版《 Warfare 1917》浏览器游戏的游戏。我被卡在了滚动部分上。

我将尝试用gif解释我想要的内容:

enter image description here

我整天都在搜索和尝试所有内容,但是我找不到任何真正的解决方案。希望您能理解我的工作。

2 个答案:

答案 0 :(得分:0)

我认为这应该可以解决问题

public class MovingCamera extends InputAdapter {

    OrthographicCamera camera;    // The camera to be moved
    float pivotX;                 // The pivot for the movement

    public MovingCamera() {
        camera = new OrthographicCamera(); // Initialize camera
    }

    // Create a pivot
    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        Vector3 unprojected = camera.unproject(new Vector3(screenX, screenY, 0)); // Convert from pixel to world coordinates
        pivotX = unprojected.x;                                                   // Save first finger touch on screen (Will serve as a pivot)
        return true;                                                              // Input has been processed
    }

    // Move the camera
    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer) {
        Vector3 unprojected = camera.unproject(new Vector3(screenX, screenY, 0)); // Convert from pixel to world coordinates
        camera.position.x += unprojected.x - pivotX;                              // Change camera position
        camera.update();                                                          // Apply changes
        return true;                                                              // Input has been processed
    }
}

在您的渲染方法中:

public void render(SpriteBatch spriteBatch) {
    spriteBatch.setProjectionMatrix(camera.combined); // Let the Sprite Batch use the camera
    spriteBatch.begin();
    // [Draw your Textures, TextureRegions, Sprites, etc...]
    spriteBatch.end();
}

答案 1 :(得分:0)

由于移动摄影机是一种不好的做法,因此最好移动Layer(子画面的容器),或者您可以尝试 ScrollPane WidgetGroup

请参见 https://libgdx.badlogicgames.com/ci/nightlies/docs/api/com/badlogic/gdx/scenes/scene2d/ui/ScrollPane.html

p.s。在1.9.11版中进行了测试