简体   繁体   中英

LibGDX - Stage.setViewport(new Viewport()) black screen

For organization's sake, I use multiple scenes for my game and rather than having each scene have a constructor that receives a Viewport (my game is scalable), I would like to set each stage's viewport separate of the constructor, then after the viewport is set, add the actors. In the main class, it would happen like this:

public void setStage(Stage s)
{
    if(currentStage != null)
        currentStage.dispose();
    currentStage = s;
    currentStage.setViewport(view);
}

To make this go fluidly, each stage has an init method that is called within an overriden setViewport:

@Override
public void setViewport(Viewport v)
{
    super.setViewport(v);
    init();
}

However, all this gives me is a black screen... I have tried updating the camera and viewport, but no avail (note that the actors are having their render methods called).

Why am I getting this black screen and how do I fix it? If it's not possible I'll just revert to using the constructor.

If I understood correctly you want to do this:

Stage stage1 = new Stage();
stage1.getViewport().update(width, height);

rather than this:

Stage stage1 = new Stage (new StretchViewport(width, height)); // It doesn't have to be   StretchViewport

In the first case (what you are trying to do) a ScalingViewport will be costructed automatically for you with dimensions of the Gdx.graphics and an orthographic camera and acts like a StretchViewport. Why not using the second case directly where you pass the viewport you want. You can always alter your viewport whenever you want by calling stage1.getViewport().update(width, height);

or by calling stage1.setViewport(width, height, false); in older Libgdx versions.

Viewport has changed recently so if you can extend Viewport class to Override the update method maybe you can achieve what you want:

public class ViewportExtendClass extends StretchViewport{

public ViewportExtendClass(float worldWidth, float worldHeight) {
    super(worldWidth, worldHeight);
}

@Override
public void update (int screenWidth, int screenHeight, boolean centerCamera) {
    super.update(screenWidth, screenHeight, centerCamera);
    // DO YOUR INITIALIZATION HERE
}

}

From your main class you create new stage :

Stage stage1 = new Stage (new ViewportExtendClass (width, height));

and then you call :

stage1.getViewport().update(width, height);

Like this you can alter stage viewport and re initialize your assets.

    @Override
    public void setViewport(Viewport v)
    {
        super.setViewport(v);

        this.getViewport().update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false);

        Camera c = this.getViewport().getCamera();
        c.position.set(c.viewportWidth/2, c.viewportHeight/2, 0);

        init();
    }

This works, but you should also be able to update the Viewport like that at the begin of your application, if you continue to use the same one. I set the position like that instead of centering because some of my Stages will be larger than the screen.

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