Here is my code
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.viewport.FitViewport;
public class MainMenuScreen extends SpaceInvaderScreen {
SpriteBatch batch;
TextureRegion background;
Stage stage;
TextButton playbutton;
float time = 0;
public MainMenuScreen(Game game){
super(game);
}
@Override
public void show(){
batch = new SpriteBatch();
batch.getProjectionMatrix().setToOrtho2D(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
background = new TextureRegion(new Texture("data/cool.jpg"), 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
stage = new Stage(new FitViewport(1280, 1080));
Skin uiskin = new Skin(Gdx.files.internal("data/skin/uiskin.json"));
playbutton = new TextButton("Play", uiskin);
playbutton.setWidth(200);
playbutton.setHeight(100);
playbutton.setPosition(980, 620);
playbutton.setBounds(playbutton.getX(), playbutton.getY(), playbutton.getWidth(), playbutton.getHeight());
playbutton.addListener( new ClickListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
game.setScreen( new GameScreen(game));
return true;
};
});
stage.addActor(playbutton);
Gdx.input.setInputProcessor(stage);
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0.15f, 0.15f, 0.2f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(background, 0, 0);
batch.end();
stage.act();
stage.draw();
time += delta;
}
@Override
public void hide(){
batch.dispose();
background.getTexture().dispose();
}
}
i'm not sure whats going on with the clicklistener as to why it doesn't work. I also tried using the touchDown method but it doesn't work either. Sorry if this sounds like a stupid question, im fairly new to the library
Just two things to try: I can see that you're building the button in the show method. Why? It makes much more sense to have a setup method and build the whole layout of the application / game / whatever in that setup method before showing anything. But this is just a shot in the dark, I don't know this framework you are using and I might be very well wrong.
Also another thing that might cause problems when you keep multiple instances of the same game! Watch out to build the game once and only once, and pass that single instance around. Why is that important? Because there might be a case that you are calling game.setScreen( new GameScreen(game));
but on the wrong instance!
Please post your SpaceInvaderScreen
as well so it might be easier to debug it.
And anyway, have you tried debugging the touchDown
method? Is it called at all?
Also two more things: ClickListener
is a class. Not an interface. So when you are overriding the touchDown
method, then you are basically bypassing the parent code in the superclass.
Try calling as the first line in the touchdown method:
super.touchDown(event, x, y, pointer, button);
Also the ClickListener documentation says:
when true is returned, the event is handled (...) This does not affect event propagation inside scene2d, but causes the Stage event methods to return true, which will eat the event so it is not passed on to the application under the stage.
So you should try returning false;
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.