Hier ist mein CodeWarum funktioniert mein Clicklistener nicht?
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 mit dem clicklistener was los bin nicht sicher, warum es nicht funktioniert. Ich habe auch versucht, die TouchDown-Methode zu verwenden, aber es funktioniert auch nicht. Sorry, wenn das klingt wie eine dumme Frage, ich bin ziemlich neu in der Bibliothek
Können Sie auch den Spiel-, GameScreen- und SpaceInvaderScreen-Code posten? –