简体   繁体   English

LibGDX-Stage.setViewport(new Viewport())黑屏

[英]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: 为了使这一过程顺利进行,每个阶段都有一个init方法,该方法在重写的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). 但是,所有这些给了我一个黑屏……我尝试更新摄影机和视口,但无济于事(请注意,actor正在调用其render方法)。

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. 在第一种情况(您要尝试执行的操作)中,ScalingViewport会自动为您构造Gdx.graphics和正交摄影机的尺寸,其行为类似于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); 您随时可以通过调用stage1.getViewport().update(width, height);随时更改视口stage1.getViewport().update(width, height);

or by calling stage1.setViewport(width, height, false); 或通过调用stage1.setViewport(width, height, false); in older Libgdx versions. 在较旧的Libgdx版本中。

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. 我这样设置位置而不是居中,因为我的某些舞台比屏幕大。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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