Search code examples
javabuttonlibgdx

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.


Solution

  • 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!