Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (798)
Games in Android Showcase (234)
games submitted by our members
Games in WIP (865)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [LibGDX] Creating menu. Button madness?  (Read 5408 times)
0 Members and 1 Guest are viewing this topic.
Offline MrPork
« Posted 2015-04-09 22:02:15 »

                 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?

1  
2  
3  
4  
5  
6  
7  
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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
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);

"f**k it, maybe it'll work." -Me
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 486
Exp: 7 years



« Reply #1 - Posted 2015-04-09 22:06:10 »

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
Offline MrPork
« Reply #2 - Posted 2015-04-09 22:10:19 »

Yay! Thank you Pizza! You've always given me such useful info!  Grin

"f**k it, maybe it'll work." -Me
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline MrPork
« Reply #3 - Posted 2015-04-10 14:27:45 »

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!  Huh Grin

"f**k it, maybe it'll work." -Me
Offline chrislo27
« Reply #4 - Posted 2015-04-10 14:40:12 »

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.
1  
2  
3  
4  
5  
6  
7  
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.
1  
2  
3  
4  
5  
6  
7  
Runnable r = new Runnable(){

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

« JGO Bitwise Duke »


Medals: 486
Exp: 7 years



« Reply #5 - Posted 2015-04-10 15:08:21 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
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

If you want to call the original method (if it has an implementation), use super.method().
Offline MrPork
« Reply #6 - Posted 2015-04-10 23:41:44 »

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.

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

"f**k it, maybe it'll work." -Me
Offline MrPork
« Reply #7 - Posted 2015-04-10 23:54:46 »

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?

1  
2  
3  
4  
5  
6  
new ChangeListener(){

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

"f**k it, maybe it'll work." -Me
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 486
Exp: 7 years



« Reply #8 - Posted 2015-04-10 23:57:01 »

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().
Offline Tekkerue

Senior Devvie


Medals: 7
Projects: 1



« Reply #9 - Posted 2015-04-11 05:17:16 »

but now I still cant figure out how to check if the mouse is hovering the button.
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.
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (34 views)
2019-09-04 15:33:17

hadezbladez (3979 views)
2018-11-16 13:46:03

hadezbladez (1444 views)
2018-11-16 13:41:33

hadezbladez (3986 views)
2018-11-16 13:35:35

hadezbladez (769 views)
2018-11-16 13:32:03

EgonOlsen (4085 views)
2018-06-10 19:43:48

EgonOlsen (4664 views)
2018-06-10 19:43:44

EgonOlsen (2753 views)
2018-06-10 19:43:20

DesertCoockie (3647 views)
2018-05-13 18:23:11

nelsongames (3849 views)
2018-04-24 18:15:36
Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!