简体   繁体   English

我如何使按钮出现和消失?

[英]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. 问题是游戏开始时还没有生成Sprite,因此结果是错误。 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. 我也正在考虑在render方法上调用createButton()方法,但由于会不断调用它,因此每帧都会生成一个新按钮。

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. Scene2d中的所有actor都具有setVisible方法。 simply try : 只需尝试:

yourButton.setVisible(true)

or 要么

yourButton.setVisible(false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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