简体   繁体   中英

Libgdx dont remove actors from Stage

i have problem with libgdx. I'm working on program, that have input for boxes and then you can chose algorhytm to sort them into truck.

在此处输入图片说明

I have problem with removig boxes. After i use this code, which should remove all actors, which are 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() . 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. Removing actors in foreach loop can cause problems. So then i use 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() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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