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.

enter image description here

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. enter image description here

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

Upvotes: 1

Views: 1133

Answers (1)

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().

Upvotes: 2

Related Questions