有没有一种方法可以在LibGDX中暂时隐藏场景小部件?

6

我需要在LibGDX中临时隐藏标签或图像,这样我就可以根据传递的值使按钮的一部分成为图像或文本。我尝试过以下方法:

public void SetScore(int score)
{
    if(score<0)
    {
        highScore.setWidth(0);
        lockImage.setWidth(50);
    }
    else
    {
        highScore.setText(Integer.toString(score));
        highScore.validate();
        lockImage.setWidth(0);
    }
}

它完全失败了,有人知道如何做吗?

1个回答

9
假设它们是标准的Scene2d小部件,当您想看到它们时,请使用setVisible(true),当您不想看到它们时,请使用setVisible(false)。
大致如下...
public void SetScore(int score)
{
    if(score<0)
    {
        highScore.setVisible(false);
        lockImage.setVisible(true);
    }
    else
    {
        highScore.setVisible(true);
        highScore.setText(Integer.toString(score));
        highScore.validate();
        lockImage.setVisible(false);
    }
}

如果它们在屏幕上占用同一空间,那么您可能需要考虑将它们放在 Stack 中。

非常感谢,太完美了!我不知道为什么之前没看到setVisible,我发誓我浏览了所有的设置。 - KatGaea

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接