(libgdx) Button doesn't work
- by StercoreCode
At the game I choose StopScreen. At this screen displays button. But if I click it - it doesn't work.
What I expect - when I press button it must restart game. At this stage must display at least a message that the button is pressed.
I tried to create new and clear project. Main class implement ApplicationListener. I put the same code in the appropriate methods. And it's works!
But if i create this button in my game - it doesn't work. When i play and go to the StopScreen, i saw button. But if i click, or touch, nothing happens.
I think that the proplem at the InputListener, although i set the stage as InputProcessor.
Gdx.input.setInputProcessor(stage);
I also try to addListener for Button as ClickListener. But it gave no results.
Or it maybe problem that i implements Screen method - not ApplicationListener or Game. But if StopScreen implement ApplicationListener, at the mainGame I can't to setScreen.
Just interests question: why button displays but nothing happens to it?
Here is the code of StopScreen if it helps find my mistake:
public class StopScreen implements Screen{
private OrthographicCamera camera;
private SpriteBatch batch;
public Stage stage; //** stage holds the Button **//
private BitmapFont font; //** same as that used in Tut 7 **//
private TextureAtlas buttonsAtlas; //** image of buttons **//
private Skin buttonSkin; //** images are used as skins of the button **//
public TextButton button; //** the button - the only actor in program **//
public StopScreen(CurrusGame currusGame) {
camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480);
batch = new SpriteBatch();
buttonsAtlas = new TextureAtlas("button.pack"); //** button atlas image **//
buttonSkin = new Skin();
buttonSkin.addRegions(buttonsAtlas); //** skins for on and off **//
font = AssetLoader.font; //** font **//
stage = new Stage();
stage.clear();
Gdx.input.setInputProcessor(stage);
TextButton.TextButtonStyle style = new TextButton.TextButtonStyle();
style.up = buttonSkin.getDrawable("ButtonOff");
style.down = buttonSkin.getDrawable("ButtonOn");
style.font = font;
button = new TextButton("PRESS ME", style); //** Button text and style **//
button.setPosition(100, 100); //** Button location **//
button.setHeight(100); //** Button Height **//
button.setWidth(100); //** Button Width **//
button.addListener(new InputListener() {
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
Gdx.app.log("my app", "Pressed");
return true;
}
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
Gdx.app.log("my app", "Released");
}
});
stage.addActor(button);
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 1, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act();
batch.setProjectionMatrix(camera.combined);
batch.begin();
stage.draw();
batch.end();
}