Kevin Bryan
Kevin Bryan

Reputation: 1858

How do I make a button appear and disappear?Libgdx

I want my button to appear whenever an object reaches a certain position, that object is a sprite that is generated every second:

public void create() {
  if(spritePosition>700) {
    buttonObj.createButton();
  } 
}

public void render() {
  if (condition==true) {
    stage.draw();
  }
}

The problem is that when the games starts no Sprite is generated yet, so the result is an error. I'm also thinking of calling the createButton() method on the render method but it would generate a new button every frame because it's called constantly.

Upvotes: 0

Views: 520

Answers (3)

Mh_Bash
Mh_Bash

Reputation: 36

All actors in Scene2d have the setVisible method. simply try :

yourButton.setVisible(true)

or

yourButton.setVisible(false);

Upvotes: 0

Alp
Alp

Reputation: 3105

How about:

public void create() {
    buttonObj.createButton();
    buttonObj.setVisible(false); 
}

public void render() {
  if (condition==true) {
    buttonObj.setVisible(true);
  }
}

Upvotes: 0

donfuxx
donfuxx

Reputation: 11323

A simple way to let your button "disappear" is to just set its position to some position outside of the visible screen area.

For example something like:

buttonObj.setPosition(-1000, -1000);

To make it visible again you can just set the real coordinates again!

Upvotes: 1

Related Questions