[LibGDX] Creating menu. Button madness?

             Well I have officially lost my sanity trying to create a menu. I managed to succesfully create some tiny button on the screen but now I have no idea wether to check if the mouse is over it, or clicked it, and until now I did not realize you could put a whole new method in a constructor?! 

              Seriously, can someone explain to me how this even works?
buttonPlay.addListener(new ChangeListener(){

			@Override
			public void changed(ChangeEvent event, Actor actor) {
				gameCore.setScreen(gameCore.gameScreen);
			}
		});

It makes absolutely no sense reading it, and I feel its an important topic I should learn for game development. Hopefully if I can learn this I can create much better things. Here’s the rest of my code, not sure if I’m making buttons completely wrong, I heard you needed a table to set the buttons position but I supposed it couldn’t hurt its mouse detection much.

stage = new Stage();
		buttonAtlas = new TextureAtlas(Gdx.files.internal("data/Resources/Gui/Buttons.atlas"));
		skin = new Skin(buttonAtlas);
		buttonStyle = new TextButtonStyle();
		buttonStyle.font = new BitmapFont();
		buttonStyle.up = skin.newDrawable("ButtonWood");
		buttonStyle.down = skin.newDrawable("ButtonWood_Down");
		
		buttonPlay = new TextButton("Play",buttonStyle);
		buttonPlay.setPosition(400, 400);
		
		buttonPlay.addListener(new ChangeListener(){

			@Override
			public void changed(ChangeEvent event, Actor actor) {
				gameCore.setScreen(gameCore.gameScreen);
			}
		});
		
		buttonExit = new TextButton("Exit",buttonStyle);
		buttonExit.setPosition(400, 300);
		buttonLoad = new TextButton("Load",buttonStyle);
		buttonLoad.setPosition(400,  200);
		buttonSave = new TextButton("Save",buttonStyle);
		buttonSave.setPosition(400, 100);
		
		stage.addActor(buttonPlay);
		stage.addActor(buttonExit);
		stage.addActor(buttonLoad);
		stage.addActor(buttonSave);

Look up “anonymous class.” It’s somewhat troubling if you haven’t run into them before.

The new ...() {...} is creating a new class (that has no name) (and an instance of it) which in your case overrides the changed method in its superclass/interface, ChangeListener.

And yes, you should generally use some kind of layout manager instead of hardcoded positions: https://github.com/libgdx/libgdx/wiki/Table

Yay! Thank you Pizza! You’ve always given me such useful info! ;D

So after reading in depth on the anonymous class, I believe I have learned it correctly. Basically its a way I can do a method of said class, and then add my own code to what it will do after it has done said method? Am I correct? I am still not fully understanding it since I’ve never seen one before so hopefully I’m going in the right direction! ??? ;D

Yes, that is correct. An more common example is with threads, when you don’t want to bother making an entire class and set up methods… you know what I mean.


Thread t = new Thread(){

   @Override
   public void run(){
      // do the things
   }
}.start();

This also works with abstract classes and interfaces. You can instantiate an abstract class or interface as long as you implement the required methods in the constructor body.


Runnable r = new Runnable(){

   @Override
   public void run(){
      // do the things
   }
}

No it’s not correct, the overridden method is called instead of the original, not after. This is the behavior of all classes.

public class Anon {
	
	public static void main(String[] args) {
		new Anon() {
			@Override
			public void method() {
				System.out.println("World");
			}
		}.method();
	}
	
	public void method() {
		System.out.println("Hello");
	}
}

[quote]World
[/quote]
If you want to call the original method (if it has an implementation), use super.method().

Welp, I still cant get the button to be clicked. I swear to goodness I could of made my own Button Class by now. It just all seems so complicated to make a single button. I read that you needed the below to check if the button is clicked but it does nothing.


buttonPlay.addListener(new ClickListener(){
			public void clicked(InputEvent event, float x, float y){
				gameCore.setScreen(gameCore.gameScreen);
			}
		});

Ok I fixed the part where the button wasn’t clicking , it works now. All I had to do was set the apps input processor as the stage, but now I still cant figure out how to check if the mouse is hovering the button. More specifically is there somewhere in the documentation where I can find all the names of these listeners that affect the buttons and that are declared like this?

new ChangeListener(){

			@Override
			public void changed(ChangeEvent event, Actor actor) {
			}
		});

Javadocs: see All Known Implementing Classes: http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/scenes/scene2d/EventListener.html

You probably want InputListener, override enter() and/or exit().

If all you need to do is change the image of the button when it is hovered over then you can do this without adding another listener by setting the “over” and “checkedOver” drawables in the ButtonStyle. However, if you need to do more stuff than just change than image then you will need to add a listener.