简体   繁体   English

没有绘制阶段

[英]Stage not being drawn

I'm pulling out my hair trying to figure out why this isn't working. 我正在拔头发,试图弄清楚为什么它不起作用。

I'm just trying to put together a basic Options menu and for some reason the Stage doesn't seem to be drawing the actors. 我只是试图将基本的“选项”菜单放在一起,由于某种原因,舞台似乎并没有吸引演员。

I've tried putting the same actor creation code into another project with a working stage and it draws fine, and I've gone over both this and the working project with a fine tooth comb looking for anything I'm missing and as far as I can tell everything stage-related in the working code is in this one too, yet all I get with this OptionsScreen.java is a blank black screen. 我尝试将相同的actor创建代码放入一个具有工作阶段的另一个项目中,并且可以正常工作,并且我已经用细齿梳仔细检查了这个项目和该工作项目,以寻找我所缺少的任何东西,直到我可以告诉工作代码中与阶段相关的所有内容,也都在此代码中,但是使用OptionsScreen.java可以获得的所有内容都是黑屏。

Here's the java file in question, OptionsScreen.java 这是有问题的Java文件OptionsScreen.java

package com.kittykazoo.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.CheckBox;
import com.badlogic.gdx.scenes.scene2d.ui.CheckBox.CheckBoxStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.utils.viewport.StretchViewport;
import com.kittykazoo.gamehelpers.ScreenHandler;

public class OptionsScreen implements Screen {

    private ScreenHandler sh;

    private Stage stage;
    private Skin skin;

    private OrthographicCamera cam;
    private ShapeRenderer shapeRenderer;
    private SpriteBatch batch;

    private Label sfxVolValue;
    private Label musicVolValue;

    public OptionsScreen(ScreenHandler sh) {

        Gdx.app.log("OptionsScreen", "Attached");

        this.sh = sh;

        cam = new OrthographicCamera();
        cam.setToOrtho(true, 960, 600);

        shapeRenderer = new ShapeRenderer();
        shapeRenderer.setProjectionMatrix(cam.combined);
        batch = new SpriteBatch();
        batch.setProjectionMatrix(cam.combined);

        stage = new Stage();
        Gdx.input.setInputProcessor(stage);

        createOptions();

    }

    private void createOptions() {

        skin = new Skin();

        Pixmap pixmap = new Pixmap(100, 100, Format.RGBA8888);
        pixmap.setColor(Color.GREEN);
        pixmap.fill();

        skin.add("white", new Texture(pixmap));

        BitmapFont bfont = new BitmapFont();
        bfont.scale(1);
        skin.add("default", bfont);

        CheckBoxStyle checkBoxStyle = new CheckBoxStyle();
        checkBoxStyle.checkboxOff = skin.newDrawable("white", Color.WHITE);
        checkBoxStyle.checkboxOffDisabled = skin.newDrawable("white",
                Color.DARK_GRAY);
        checkBoxStyle.checkboxOn = skin.newDrawable("white", Color.WHITE);
        checkBoxStyle.checkboxOnDisabled = skin.newDrawable("white",
                Color.DARK_GRAY);
        checkBoxStyle.checked = skin.newDrawable("white", Color.WHITE);
        checkBoxStyle.font = skin.getFont("default");
        skin.add("default", checkBoxStyle);

        TextButtonStyle textButtonStyle = new TextButtonStyle();
        textButtonStyle.up = skin.newDrawable("white", Color.DARK_GRAY);
        textButtonStyle.down = skin.newDrawable("white", Color.DARK_GRAY);
        textButtonStyle.checked = skin.newDrawable("white", Color.BLUE);
        textButtonStyle.over = skin.newDrawable("white", Color.LIGHT_GRAY);
        textButtonStyle.font = skin.getFont("default");
        skin.add("default", textButtonStyle);

        final CheckBox checkBox = new CheckBox("Checkbox here", checkBoxStyle);
        checkBox.setPosition(100, 100);
        stage.addActor(checkBox);

        final TextButton textButton = new TextButton("UPDATE", textButtonStyle);
        textButton.setPosition(200, 200);
        stage.addActor(textButton);

        textButton.addListener(new ChangeListener() {
            public void changed(ChangeEvent event, Actor actor) {
                textButton.setText("Submitting...");
                sh.hideOptions();
            }
        });

    }

    @Override
    public void render(float delta) {

        // Black background
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        stage.act(Gdx.graphics.getDeltaTime());
        stage.draw();

    }

    @Override
    public void resize(int width, int height) {
        Gdx.app.log("OptionsScreen", "resizing");
        stage.setViewport(new StretchViewport(width, height));
    }

    @Override
    public void show() {
        Gdx.app.log("OptionsScreen", "show called");
    }

    @Override
    public void hide() {
        Gdx.app.log("OptionsScreen", "hide called");
    }

    @Override
    public void pause() {
        Gdx.app.log("OptionsScreen", "pause called");
    }

    @Override
    public void resume() {
        Gdx.app.log("OptionsScreen", "resume called");
    }

    @Override
    public void dispose() {
        stage.dispose();
        skin.dispose();
    }

}

I assume I must be missing something super obvious. 我想我一定会错过一些非常明显的东西。 If anyone can tell me where I'm going wrong I would be very grateful! 如果有人可以告诉我我要去哪里错了,我将不胜感激!

public void resize(int width, int height) {
    stage.setViewport(new StretchViewport(width, height));
}

This is completely wrong in many ways. 在许多方面这是完全错误的。 First of all, you do not want to create a new viewport on every resize, because of the Garbage Collector. 首先,由于Garbage Collector,您不想在每次调整大小时都创建新的视口。

Furthermore creating a StretchViewport with the new screen width and height renders the StretchViewport useless, because it will basically behave like a ScreenViewport instead. 此外,使用新的屏幕宽度和高度创建一个StretchViewport会使StretchViewport失去作用,因为它的行为基本上类似于ScreenViewport Make sure to read the Viewports wiki article to understand how the different viewports work. 确保阅读“ 视口” Wiki文章,以了解不同视口的工作方式。

And last but not least, the reason why your stage is not drawn is also hidden within that. 最后但并非最不重要的一点是,您的舞台未绘制的原因也隐藏在其中。 A UI Stage needs to have the camera centered, otherwise it won't be rendered correctly. UI Stage需要将相机居中,否则将无法正确渲染。

So what you want to do instead is the following: 因此,您要做的是以下内容:

public void resize(int width, int height) {
    stage.getViewport().update(width, height, true);
}

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

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