0

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();
 }
}
eebbesen
5,1558 gold badges52 silver badges73 bronze badges
asked Dec 22, 2016 at 14:17

1 Answer 1

1

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.

answered Dec 22, 2016 at 15:38
Sign up to request clarification or add additional context in comments.

13 Comments

Thank you for your answer ! Maybe if we have time(because we are really late), i will modify what you criticized. I have another question. If i choose to keep the renderer, i have to put the button with the InputMultiplexer in the renderer or in the GameScreen?
Almost- you put the button in the stage, and the stage in the InputMultiplexer. You would do this mapping in GameScreen.
it does not work. :'( I'm sorry I'm French and not very good on libgdx. If it's easier, I can send you my code directly, so you can tell me and explain the problem.
I tried again and here is what I get: The game without modification:link The game with inputMultplexer: link We have the button but can not click on it and the checkerboard is bigger
The InputMultiplexer alone won't change how things are rendered- it must be one of the other changes you made. Is your code online somewhere? I'm not sure StackOverflow was really intended for this type of discussion.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.