This site requires JavaScript, please enable it in your browser!
Greenfoot back
timothytitus
timothytitus wrote ...

2017/3/19

Removing all objects but some

timothytitus timothytitus

2017/3/19

#
Pretty self-explanatory, want to remove some objects but the player, healthbar... What is the best way to do this?
timothytitus timothytitus

2017/3/19

#
List<Actor> actors = getObjects(null);
        for (Actor obj : actors) 
        {
            if ((!obj.equals(Healthbar.class)) && (!obj.equals(Player.class)))
            {
                removeObject(obj);
        }
    }
I'm trying this code but it doesn't seem to work
Nosson1459 Nosson1459

2017/3/19

#
You can use the removeObjects method for each of the classes that you want to be removed, everything that you want to stay just leave alone.
// in the World subclass in the place you want to do the removing
removeObjects(getObjects(FirstClassName.class)); // one type of class to remove
removeObjects(getObjects(SecondClassName.class)); // another type of class to remove
removeObjects(getObjects(ThirdClassName.class)); // a third type of class to remove
I just saw your code. I don't think you can compare classes like that.
Nosson1459 Nosson1459

2017/3/19

#
For your code you would probably have to do:
List<Actor> actorsList = getObjects(null);
for (Actor actor : actorsList) if (! (actor instanceof Healthbar) && ! (actor instanceof Player)) removeObject(actor);
You need to login to post a reply.