简体   繁体   中英

Libgdx How to set viewport position?

I know that in libgdx a stage uses a viewport which defines the camera. I want to render a stage just on a small square on my screen. I tried using a customized viewport but I only managed to set it's size. I also tried using setPosition(int screenX, int screenY) method, but it seems that the viewport is always centered on the middle of the screen no matter what I try. Does anyone know how to set a viewport's position on the screen?

Your problem in my opinion is not in setting viewport position but in updating it - you've got to center your camera when viewport updating

    stage = new Stage();
    viewport = new ExtendViewport(WIDTH, HEIGHT);
    viewport.setScreenPosition(500, 500); //I'm setting viewport's position

    ...

    @Override
    public void render(float delta) 
    {
        ...

        viewport.update(currentWindowWidth, currentWindowHeight, true); //here I'm updateing it with setting camera center

        ...
    }

Compare

void update(int screenWidth, int screenHeight, boolean centerCamera)

and

void update(int screenWidth, int screenHeight)

which calls the update version above with false as default

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM