简体   繁体   中英

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.

Here's the java file in question, 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.

Furthermore creating a StretchViewport with the new screen width and height renders the StretchViewport useless, because it will basically behave like a ScreenViewport instead. Make sure to read the Viewports wiki article to understand how the different viewports work.

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.

So what you want to do instead is the following:

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

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