Libgdx 不要从舞台上移除演员

Libgdx dont remove actors from Stage

我对 libgdx 有疑问。我正在研究程序,它有盒子的输入,然后你可以选择 algorhytm 将它们分类到卡车中。

我在删除框时遇到问题。在我使用此代码后,它应该删除所有演员,即纹理(框)。

for(Actor actor : stage.getActors()){
        if(actor.getClass() == Textures.class){
            actor.remove();
        }
    }

排序算法运行良好,所有箱子都在卡车上,但它不会移除一些旧箱子。

然后我尝试使用 actor.getName() 删除它们。同样的结果。还有创建演员的代码:

for(Actor actor : stage.getActors()){
        if(actor.getName()!=null){
            if(actor.getName().equals("shape")){
                actor.remove();
            }
        }
    }

    //create actors
    for (ShapeMeasurments sh:shapes) {
        Textures textures = new Textures((sh.getX()*1.45f+30),sh.getY()*1.45f,sh.getWidth()*1.45f,
                sh.getHeight()*1.45f,sh.getMaterial());
        textures.setName("shape");
        stage.addActor(textures);
    }

我发现了一个问题。 actor.remove() 是 foreach 循环中的问题。在 foreach 循环中删除 actors 可能会导致问题。所以我使用 actor.addAction(Actions.removeActor()); 并且它有效。在 second answer

中说过

并且不要使用 if(actor.getClass() == Textures.class) 比较 类 不是微不足道的操作,您应该使用 actor.setName()actor.getName()