Palipica
Palipica

Reputation: 1

Display button libgdx

I'm a beginner on LibGDX, and we currently work in group of 4 to make a Checkers game. It's a project for school. We blocked to make the movement of pawns but it's not the problem. We have an architecture MVC. On the game screen, we have a board game with pawns. We can not display a pawn, while on other screens it was easy. I can make it appear but on the game board but it is not clickable. When I add

stage.getViewport ().update (width, height, true)

the button appears outside of the game board but not clickable and the game board is zoomed.

Here is a link of the various classes

The problem is on the class GameScreen. When I click on play which is located on class JouerIA or JouerJoueur, I do a setScreen(GameScreen).

So I paste here only GameScreen and GameRenderer. If you need more code to help me, I can paste more.

Here is the code without my different additions of buttons.

GameScreen.java

public class GameScreen implements Screen {

private GameRenderer renderer;
public static String couleur1, couleur2, nom1, nom2;
Stage stage;
Skin skinneon;
OrthographicCamera camera;
TextButton menu;


@Override
public void render(float delta) {

    this.renderer.render(delta);

}

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

    this.renderer.setSize(width, height);

}

@Override
public void show() {
    Plateau plateau;
    ControlPlateau controller;

    Assets.loadGame();

    plateau = new Plateau();
    plateau.ajoutPions();
    controller = new ControlPlateau(plateau);

    this.renderer = new GameRenderer(plateau, controller);
    this.renderer.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());


}

@Override
public void hide() {
    this.renderer.dispose();
    Assets.disposeGame();
}

@Override
public void pause() {
}

@Override
public void resume() {
}

@Override
public void dispose() {

}

}

GameRenderer.java

public class GameRenderer implements Renderer {

    private final Stage stage = new Stage(new FitViewport(10, 10));
    private Table hud;

    protected ControlPlateau controller;


    public GameRenderer(Plateau plateau, ControlPlateau controller) {



    Gdx.input.setInputProcessor(this.stage);
    this.stage.addActor(plateau);
    this.controller = controller;



        /*addListener(new InputListener() {
            public void actionPerformed(InputEvent event) {
               controller.tap();
            }
        });*/
        }

        @Override
        public void render(float delta) {
         Plateau plateau = new Plateau();

         Gdx.gl.glClearColor(.3f, .3f, .4f, 1);
         Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
         this.stage.draw();
         Gdx.input.setInputProcessor(stage);
         Gdx.input.setInputProcessor(new GestureDetector(new ControlPlateau(plateau)));
    }

    @Override
    public void setSize(int width, int height) {
        this.stage.getViewport().update(width, height, false);
        Gdx.graphics.requestRendering();
    }

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

}

Upvotes: 0

Views: 750

Answers (1)

John
John

Reputation: 1490

Welcome to Stack Overflow!

The short answer is that you are reseting the input processor several times, so it doesn't get input from the stage. Gdx.input.setInputProcessor() can only handle one input processor at a time. If you need to collect input from multiple sources, you want to create an [InputMultiplexer][1] class, give it all your input processors (i.e. stage, GestureDetector, etc.), then pass the InputMultiplexer to Gdx.input.setInputProcessor()- you can find more on the libGDX wiki.

Now if you will permit me to critique your form, there are a few other things you may want to consider:

First: I'm a fair-weather MVC fan, but I would propose that you may save yourself some headaches if you think of your GameScreen.java class as your View (after all, a screen is a view)- you can then put your rendering code into the Screen's render() method and avoid creating a separate GameRenderer class. In my games, I have all my setup code in the Controller (think app controller, not gamepad controller or keyboard controller), and then each Screen gets a reference to the Controller and Model.

Second: In your renderer (should you choose to keep it), you'll want to avoid instantiating new objects in your render method. In the code you have above:

@Override
public void render(float delta) {
  Plateau plateau = new Plateau();

  Gdx.gl.glClearColor(.3f, .3f, .4f, 1);
  Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
  this.stage.draw();
  Gdx.input.setInputProcessor(stage);
  Gdx.input.setInputProcessor(new GestureDetector(new ControlPlateau(plateau)));
}

This will create a new Plateau object, a new GextureDetector, and a new ControlPlateau object every time render() is called- potentially 60 times a second! I strongly recommend moving these lines of code into a show() method so that your instantiation is only done once.

Hopefully that helps- let me know if you need further clarification.

Upvotes: 1

Related Questions