0
\$\begingroup\$

guys!

I divided the screen into two parts using Viewport and now the game does not work correctly.

  1. The control buttons do not work all.
  2. If you click in some part of the screen (not on the control) - this will change the direction of movement of the snake.
  3. The reaction of the snake to any action is slow.

Here is my code. I understand that the problem is with the render method, but what is it?

public class GameScreen implements Screen {
    private Main parent;
    private Stage stage;
    public static SnakeControl snakeControl;
    private SpriteBatch batch;
    private GameControl game;

    public GameScreen(Main main) {
        parent = main;

        stage = new Stage(new ScreenViewport());
        Gdx.input.setInputProcessor(stage);
        snakeControl = new SnakeControl();

        GameAssets.instance().loadAssets();
        batch = new SpriteBatch();
        game = new GameControl();
    }

    @Override
    public void show() {
        Table table = new Table();
        table.setFillParent(true);
        table.setDebug(true);
        table.add(snakeControl.get_tSnakeControl()).fillX().pad(0f, 5f, 0f, 5f);
        stage.addActor(table);
    }

    @Override
    public void render(float delta) {

        clearScreen();
        Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()/3);
        stage.act(Gdx.graphics.getDeltaTime());
        stage.draw();

        Gdx.gl.glViewport(0, Gdx.graphics.getHeight()/3, Gdx.graphics.getWidth(), (Gdx.graphics.getHeight() - Gdx.graphics.getHeight()/3));
        game.update(Gdx.graphics.getDeltaTime());
        batch.begin();
        game.render(batch);
        batch.end();
    }

    private void clearScreen() {
        Gdx.gl.glClearColor(0f, 0f, 0f, 1f);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    }

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

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void dispose() {
        stage.dispose();
        batch.dispose();
        GameAssets.instance().dispose();
    }
}

enter image description here

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.