简体   繁体   中英

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.

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!

How about:

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

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

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

yourButton.setVisible(true)

or

yourButton.setVisible(false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM