How do I create a good play button in libGDX?

Hi there,

I’ve recently started practicing with LibGDX and have come across a small problem. I’m trying to create a simple play button for the android app’s menu screen. Similar to that of say, Flappy Bird. I want it so that whenever I press the button, it does something like moves down by 4, then back up, then goes to the next menu. I already have a screen system setup, so just refer to the set menu as simply: “gsm.setState(PLAY);”.

Specifically, when you touch the button, it does a small animation type thing, then proceeds to switch the screen.

Thank you!

  • A

It’s simple really.

What you need to learn about are button states. Basically, when you have clicked the mouse button down on the button, the button should move down 4 pixels. Then, when the mouse is released, move the button back and perform the screen swap, or any other actions.

So in order:
-Check if mouse button is PRESSED.
-If pressed and collides with button, move the button
-Every update cycle, check to see if the mouse button has just been RELEASED. You can do this by creating a boolean, and setting it to true when the mouse button is pressed. Then, check every update to see if that boolen is true and if the button is released. Set the boolean to false, and perform your button action.

Thank you for the quick follow up!

Here is what I did.

		if(btnPressed == true){
			y = y + 4;
			btnPressed = false;
		}else if(btnPressed == false)
			y = 800 / 2 - 188 / 2;
		

I did try this prior to making the post. It works and all, but I’m stumped. I want it to wait like one second before coming back up. Then after it comes up, proceed.
Any ideas?

Thanks!

Do you want the button to come up after one second? If so, do this.


boolean taskUpScheduled = false;
if(btnPressed == true){
	         y = y + 4;
	         btnPressed = false;
	      }else if(btnPressed == false && !taskUpScheduled) {
taskUpScheduled = true;
	    	 Timer.schedule(new Task() {
				
				@Override
				public void run() {
					 y = 800 / 2 - 188 / 2;
taskUpScheduled = false;
				}
			}, 1); 
	      }

If you mean you want to set the screen after a second, you can do Timer.schedule(Timer.task, time delay), and set the screen in the Timer.task. Sorry I can’t type the code properly right now because I’m on an iPad. But basically, just use Timer.schedule when you want to run some code after a time delay.

If you’re using libGDX, Actions would be best suited to timing gui events.