如何在桌面模式下修复libgdx闪烁屏幕?

时间:2016-07-23 09:47:11

标签: java android libgdx

当我运行模拟器并启动应用程序时,我看到闪烁屏幕,但在DesktopLauncher屏幕上的PC上是正常的。 我无法理解我犯错误的地方。 渲染方法覆盖,但它没有帮助。

class starter:

        public class Drop extends Game {
    Camera cam;
    Game game;
    SpriteBatch batch;
    int tempGameScore = 0;
    int dropsGatchered = 0;
    Preferences preferences;//сохраняем игру

    @Override
    public void create() {
        batch = new SpriteBatch();
     this.setScreen(new MainMenuScreen(this));
    }

    @Override
    public void render() {

        super.render();

    }

    @Override
    public void dispose() {
        batch.dispose();

    }

的MainMenu: 我认为我在使用场景工具时出错了。 是否有必要画一个场景?

    public class MainMenuScreen implements Screen {
    Sprite texture;
    final Drop game;
    Skin skin;
    Stage stage;
    OrthographicCamera camera;
    ImageButton newGameButton, exit, highScore;
    Table table;
    ImageButton.ImageButtonStyle btnplayStyle, btnscoreStyle, btnexitStyle;

    private static TextureAtlas atlas, backAtlas;
    public MainMenuScreen(final Drop gam) {
        this.game = gam;

        atlas = new TextureAtlas(Gdx.files.internal("texture/texture.pack"), true);
        backAtlas = new TextureAtlas(Gdx.files.internal("texture/background.pack"), true);
        texture = new Sprite(backAtlas.findRegion("background"));
        skin = new Skin();
        skin.addRegions(atlas);
        table = new Table();

        camera = new OrthographicCamera();

        camera.setToOrtho(false, 800, 480);
        stage = new Stage();
        stage.addActor(table);
        Gdx.input.setInputProcessor(stage);// Make the stage consume events
        table();
    }

    private void table() {
        btnplayStyle = new ImageButton.ImageButtonStyle();
        btnplayStyle.up = skin.getDrawable("play");//кнопка не нажата
        btnplayStyle.over = skin.getDrawable("play");
        btnplayStyle.down = skin.getDrawable("play"); // кнопка нажата
        newGameButton = new ImageButton(btnplayStyle);
        newGameButton.setSize(300, 200);

        stage.addActor(newGameButton);
        newGameButton.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                game.setScreen(new GameScreen(game));
            }
        });

        //Button score
        btnscoreStyle = new ImageButton.ImageButtonStyle();
        btnscoreStyle.up = skin.getDrawable("records");//кнопка не нажата
        btnscoreStyle.over = skin.getDrawable("records");
        btnscoreStyle.down = skin.getDrawable("records"); // кнопка нажата
        highScore = new ImageButton(btnscoreStyle);
        highScore.setSize(300, 200);

        stage.addActor(highScore);
        highScore.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {

                game.setScreen(new Score(game) {

                });

            }
        });

        //Button EXIT
        btnexitStyle = new ImageButton.ImageButtonStyle();
        btnexitStyle.up = skin.getDrawable("exit");//кнопка не нажата
        btnexitStyle.over = skin.getDrawable("exit");
        btnexitStyle.down = skin.getDrawable("exit"); // кнопка нажата
        exit = new ImageButton(btnexitStyle);
        exit.setSize(300, 200);

        stage.addActor(exit);
        exit.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                Gdx.app.exit();
            }
        });
        table.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        game.batch.begin();
        game.batch.draw(texture, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

        table.add(newGameButton).width(400).height(120);
        table.getCell(newGameButton).spaceBottom(30);
        table.row();

        table.add(highScore).width(400).height(120);

        table.getCell(highScore).spaceBottom(30);
        table.row();

        table.add(exit).width(400).height(120);
        table.getCell(exit).spaceBottom(30);
        table.row();
        game.batch.end();
    }

    @Override
    public void render(float delta) {

        stage.act();
        stage.draw();
    }


    @Override
    public void resize(int width, int height) {

    }

    @Override
    public void show() {

    }


    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {

    }

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

我认为我在使用场景工具

时出错了

1 个答案:

答案 0 :(得分:1)

在MainMenuScreen中,您必须在render()方法中绘制内容,而不是在show()方法中绘制内容。像这样:

@Override
public void render() {
   stage.draw();
   // ... possibly more drawing code
}