簡體   English   中英

你如何使用 LibGDX 實現水平滾動?

[英]How do you implement Horizontal-Scrolling with LibGDX?

我想編寫一個類似於舊的“戰爭 1917”瀏覽器游戲的游戲。 我被困在滾動部分。

我將嘗試用 gif 解釋我想要的內容:

在此處輸入圖片說明

我已經搜索並嘗試了一整天,但我找不到任何真正的解決方案。 我希望你能理解我想要做什么。

我認為這應該可以解決問題

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

由於移動相機是一個不好的做法,最好移動一個圖層(你的精靈的容器)或者你可以嘗試ScrollPaneWidgetGroup的實現

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

ps 在 v. 1.9.11 上測試

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM