I am trying to put a button using a play image. I have followed some tutorials and cannot find the documentation to complete my project. The game is actually done, and I just need to connect the gameover screen to the game screen so I can replay the game by pushing the play image. I got stuck in this longer than I expected. I cannot change the methods because I will mess up my code. Code and images below.
package com.mygdx.game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.utils.viewport.FitViewport;
public class GameOverScreen extends BaseScreen {
private Image background;
private Stage stage;
private Image gameOver;
private Image replay;
public GameOverScreen(final MainGame game) {
super(game);
stage = new Stage(new FitViewport(640, 320));
//Preparing actors
background = new Image(game.getManager().get("bg.png", Texture.class));
gameOver = new Image(game.getManager().get("gameover.png", Texture.class));
replay = new Image(game.getManager().get("plybtn.png", Texture.class));
replay.addCaptureListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
game.setScreen(game.gameScreen);
}
});
//Giving their positions and sizes
background.setPosition(0,0);
gameOver.setPosition(320 - gameOver.getWidth()/2, 180 - gameOver.getHeight()/2);
replay.setPosition(320 - replay.getWidth()/2, 60 -replay.getHeight()/2);
//Set them in stage
stage.addActor(background);
stage.addActor(gameOver);
stage.addActor(replay);
}
@Override
public void show() {
Gdx.input.setInputProcessor(stage);
}
@Override
public void hide() {
Gdx.input.setInputProcessor(null);
}
@Override
public void dispose() {
stage.dispose();
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0.4f, 0.5f, 0.8f, 1f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act();
stage.draw();
}
}
[This is the screen that I get, it does not change the screen when I touch the button][1]
[1]: https://i.stack.imgur.com/GxVLM.png
Update: This is the solution to my problem in case you need it.
// Declare statements at the beginning of the class
private Texture myTexture;
private TextureRegion myTextureRegion;
private TextureRegionDrawable myTexRegionDrawable;
private ImageButton pbutton;
// Make a texture out of the image in the MenuScreen constructor
myTexture = new Texture(Gdx.files.internal("plybtn.png"));
myTextureRegion = new TextureRegion(myTexture);
myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
pbutton = new ImageButton(myTexRegionDrawable);
The event doesn't look like its being received. Change
replay.addCaptureListener(new ChangeListener() {
to
replay.addListener(new ChangeListener() {
or even use ClickListener
instead. CaptureListener
is to intercept events to disable them, not for handling and you need to return true in the capture handler to not break event handling down the tree.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.