[英]Libgdx dont remove actors from Stage
i have problem with libgdx. 我有libgdx问题。 I'm working on program, that have input for boxes and then you can chose algorhytm to sort them into truck. 我正在研究程序,输入框,然后可以选择algorhytm将其分类为卡车。
I have problem with removig boxes. 我在removig box上遇到问题。 After i use this code, which should remove all actors, which are Textures(boxes). 使用此代码后,应删除所有actor,即Textures(boxes)。
for(Actor actor : stage.getActors()){
if(actor.getClass() == Textures.class){
actor.remove();
}
}
Sorting algorithm work good and all boxes are in the truck, but it doesn't remove some of the old boxes. 排序算法运行良好,所有箱子都在卡车上,但它不会删除一些旧箱子。
Then i try to use delete them by actor.getName()
. 然后我尝试使用actor.getName()
删除它们。 Same result. 结果相同。 There is also code whitch create actors: 还有一些代码创建者:
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);
}
I found a problem. 我发现了一个问题。 actor.remove()
is problem in foreach loop. actor.remove()
是foreach循环中的问题。 Removing actors in foreach loop can cause problems. 在foreach循环中删除参与者可能会导致问题。 So then i use actor.addAction(Actions.removeActor());
所以然后我使用actor.addAction(Actions.removeActor());
and it works. 而且有效。 That was said in second answer 那是第二答案
And don't use if(actor.getClass() == Textures.class)
comparing classes is not trivial operation, you should use actor.setName()
and actor.getName()
. 并且不要使用if(actor.getClass() == Textures.class)
比较类并非无关紧要的操作,您应该使用actor.setName()
和actor.getName()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.