libgdx - clicklistener on button

Hi all,

I am starting to do my first steps with libgdx and got realy fast first problems :slight_smile:

my button eventlistener will not work. Here is my class.

package com.mygdx.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;

import java.awt.event.InputEvent;

public class MyGdxGame_test2 extends ApplicationAdapter {
	SpriteBatch batch;
	Texture img;
	private TextButton exitButton;
	private Stage stage;
	private BitmapFont font;



	@Override
	public void create () {


		stage= new Stage();
		Gdx.input.setInputProcessor(stage);

		batch = new SpriteBatch();
		img = new Texture("textures/title.png");

		TextButton.TextButtonStyle buttonStyle = new TextButton.TextButtonStyle();
		buttonStyle.fontColor = Color.BLACK;
		font = new BitmapFont(Gdx.files.internal("fonts/plaincred.fnt"),false);
		buttonStyle.font =font; 
		exitButton = new TextButton("Exit", buttonStyle);
		exitButton.addListener(new ClickListener() {

			public void clicked(InputEvent event, float x, float y, int pointer, int button) {
				System.out.println("X:" + x + " Y:" + y);
				//return true;
			}

			public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
				System.out.println("X:" + x + " Y:" + y);
				return true;
			}

			public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
				System.out.println("touchup");
			}
		});
		stage.addActor(exitButton);
		System.out.println("Start");
	}

	@Override
	public void render () {
		float t = Gdx.graphics.getDeltaTime();
		Gdx.gl.glClearColor(1, 1, 1, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		batch.begin();
		batch.draw(img, 0, 0, stage.getWidth(), stage.getHeight());
		batch.end();
		stage.act(t);
		stage.draw();
	}
}

I was serching at the web, but i dont know what i have to change.

I setted the InputProcessor, I added the clicklistener, tried touchdown, up and click events but nothing helpes.
is there anything else I have to configure or start? maybe on the android class part or manifest?

Please help.

I don’t know what IDE you are using, but it doesn’t seem to assist you very much. You didn’t implement (override) the right function. :point:

exitButton.addListener(new ClickListener() {

    @Override
    public void clicked(InputEvent event, float x, float y) {
        System.out.println("X:" + x + " Y:" + y);
        //return true;
    }

});

hi, thanks for your answer this didnt solve my problem 100% but give me the hint for it.

I am using Android Studio, and on calling the arguments it doesnt take the ‘Inputevent event’ from com.badlogic.gdx.scenes.scene2d.InputEvent but of java.awt.event.InputEvent.

Thats why I couldt @Override it, just on a call of Super.TouchDown(InputEvent event, float x, float y, int pointer, int button) I got the hint from Android Studio regarding wong InputEvent envent arguments

after changing the code:

public boolean touchDown(com.badlogic.gdx.scenes.scene2d.InputEvent event, float x, float y, int pointer, int button) {
				System.out.println("X:" + x + " Y:" + y);
				return true;
			}

it works with @Override and without.

Any ideas why its prefering the java class there and how i can avoid this to call the Listener with short InputEvent arguments?
This are the first steps for me with java and im very insecure with class callings and using :persecutioncomplex: