简体   繁体   English

LibGDX窗口中有一个单元困扰我

[英]There is a Cell bothering me in a LibGDX Window

I think I might have found an issue in LibGDX, but before opening an issue in their GitHub, I thought it would be better to discuss it here because maybe I am wrong. 我想我可能在LibGDX中发现了一个问题,但是在他们的GitHub上发布问题之前,我认为最好在这里进行讨论,因为也许我错了。

This is what is happening to me: 这就是我正在发生的事情:

I have a class named Information extending Window , where I show the information about the characters of my game. 我有一个名为“ Information扩展Window的类,在其中显示有关游戏角色的信息。 Since I need to update the labels dynamically, I thought it would be better to simply remove all the actors inside the Window each time I call setVisible(false) . 由于我需要动态更新标签,因此我认为每次调用setVisible(false)时都简单地删除Window中的所有actor会更好。 It worked before. 它曾经工作过。 So this is my code: 这是我的代码:

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);
}   

So, every time I call setVisible , I create or erase the actors. 因此,每次调用setVisible ,我都会创建或删除参与者。 The thing is that the first time I call the method, it is working perfectly, but the second and successive times, it appears a Cell with no info besides the Name of the character. 问题是,我第一次调用该方法时,它运行良好,但是第二次或连续几次,它出现了一个除字符名称外没有任何信息的Cell

I debugged the creation of the Actors and the deletion of the same and all seems to be working perfectly. 我调试了Actors的创建并删除了Actors ,所有这些似乎都运行良好。

So I was about to open an issue in LibGDX's GitHub, but if someone knows why this is happening to me, I will prefer it. 所以我本来打算在LibGDX的GitHub上发布一个问题,但是如果有人知道为什么会这样,我会更喜欢它。

Thanks in advance! 提前致谢!

Ok! 好! So I am responding my own question in case someone needs it. 所以我在回答自己的问题,以防有人需要。

Thanks to Tenfour04 for his comment, since it helped me to find it. 感谢Tenfour04的评论,因为它帮助我找到了它。

The correct method to call was clearChildren() . 正确的调用方法是clearChildren() This is my new code: 这是我的新代码:

    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);
}

Hope this helps someone else in the future. 希望这对以后的人有所帮助。

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

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