Search code examples
javaandroidlibgdx

How fix in libgdx blinks screen in Desktop Mode?


when i run emulator and start application i see blinks screen, but on a PC in DesktopLauncher screen is normal. I can not understand where I made mistakes. render method override but it does not help.

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: I think I'm working wrong with the scene tool. Can it is necessary as otherwise to draw a scene?

    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();
    }
}

I think I'm working wrong with the scene tool


Solution

  • In MainMenuScreen you have to draw things in the render() method, not in the show() method. Like this:

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