在 LibGDX Window 中有一个 Cell 困扰着我
There is a Cell bothering me in a LibGDX Window
我想我可能在 LibGDX 中发现了一个问题,但在他们 GitHub 中打开一个问题之前,我认为最好在这里讨论它,因为也许我错了。
这就是发生在我身上的事情:
我有一个名为 Information
的 class 扩展 Window
,我在其中显示有关游戏角色的信息。由于我需要动态更新标签,我认为每次调用 setVisible(false)
时简单地删除 Window
内的所有演员会更好。它以前有效。所以这是我的代码:
public class Information extends Window {
private final static String TAG = Information.class.getName();
private Unit calledBy; //Variable to know who is calling
public Information (Skin skin) {
//Setting the attributes
super("Information", skin);
this.setPosition(0, 0);
this.setBounds(this.getX(), this.getY(), Constants.INFORMATION_WIDTH, Constants.INFORMATION_HEIGHT);
this.setVisible(false);
}
@Override
public void setVisible(boolean visible) {
if (!visible){
SnapshotArray<Actor> actors = this.getChildren();
while (actors.size > 0){
actors.get(0).remove();
}
} else {
this.add(new Label("Name: " + getCalledBy().getName(), getSkin()));
this.row();
this.add(new Label("Attack: " + getCalledBy().getAttack(), getSkin()));
this.row();
this.add(new Label("Defense: " + getCalledBy().getDefense(), getSkin()));
this.row();
this.add(new Label("Health: " + getCalledBy().getHealth(), getSkin()));
}
super.setVisible(visible);
}
因此,每次调用 setVisible
时,我都会创建或删除演员。问题是,我第一次调用该方法时,它运行良好,但第二次和连续两次,它出现 Cell
除了角色名称之外没有任何信息。
我调试了 Actors
的创建和删除,似乎一切正常。
所以我正要在 LibGDX 的 GitHub 中打开一个问题,但如果有人知道为什么这会发生在我身上,我会更喜欢它。
提前致谢!
好的!所以我正在回答我自己的问题,以防有人需要它。
感谢 Tenfour04 的评论,帮我找到了。
正确的调用方法是 clearChildren()
。这是我的新代码:
public void setVisible(boolean visible) {
if (!visible){
this.clearChildren();
} else {
this.add(new Label("Name: " + getCalledBy().getName(), getSkin()));
this.row();
this.add(new Label("Attack: " + getCalledBy().getAttack(), getSkin()));
this.row();
this.add(new Label("Defense: " + getCalledBy().getDefense(), getSkin()));
this.row();
this.add(new Label("Health: " + getCalledBy().getHealth(), getSkin()));
}
super.setVisible(visible);
}
希望这对以后的其他人有所帮助。
我想我可能在 LibGDX 中发现了一个问题,但在他们 GitHub 中打开一个问题之前,我认为最好在这里讨论它,因为也许我错了。
这就是发生在我身上的事情:
我有一个名为 Information
的 class 扩展 Window
,我在其中显示有关游戏角色的信息。由于我需要动态更新标签,我认为每次调用 setVisible(false)
时简单地删除 Window
内的所有演员会更好。它以前有效。所以这是我的代码:
public class Information extends Window {
private final static String TAG = Information.class.getName();
private Unit calledBy; //Variable to know who is calling
public Information (Skin skin) {
//Setting the attributes
super("Information", skin);
this.setPosition(0, 0);
this.setBounds(this.getX(), this.getY(), Constants.INFORMATION_WIDTH, Constants.INFORMATION_HEIGHT);
this.setVisible(false);
}
@Override
public void setVisible(boolean visible) {
if (!visible){
SnapshotArray<Actor> actors = this.getChildren();
while (actors.size > 0){
actors.get(0).remove();
}
} else {
this.add(new Label("Name: " + getCalledBy().getName(), getSkin()));
this.row();
this.add(new Label("Attack: " + getCalledBy().getAttack(), getSkin()));
this.row();
this.add(new Label("Defense: " + getCalledBy().getDefense(), getSkin()));
this.row();
this.add(new Label("Health: " + getCalledBy().getHealth(), getSkin()));
}
super.setVisible(visible);
}
因此,每次调用 setVisible
时,我都会创建或删除演员。问题是,我第一次调用该方法时,它运行良好,但第二次和连续两次,它出现 Cell
除了角色名称之外没有任何信息。
我调试了 Actors
的创建和删除,似乎一切正常。
所以我正要在 LibGDX 的 GitHub 中打开一个问题,但如果有人知道为什么这会发生在我身上,我会更喜欢它。
提前致谢!
好的!所以我正在回答我自己的问题,以防有人需要它。
感谢 Tenfour04 的评论,帮我找到了。
正确的调用方法是 clearChildren()
。这是我的新代码:
public void setVisible(boolean visible) {
if (!visible){
this.clearChildren();
} else {
this.add(new Label("Name: " + getCalledBy().getName(), getSkin()));
this.row();
this.add(new Label("Attack: " + getCalledBy().getAttack(), getSkin()));
this.row();
this.add(new Label("Defense: " + getCalledBy().getDefense(), getSkin()));
this.row();
this.add(new Label("Health: " + getCalledBy().getHealth(), getSkin()));
}
super.setVisible(visible);
}
希望这对以后的其他人有所帮助。