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

2015/4/19

How to remove everything except for one actor

dyl_fisher dyl_fisher

2015/4/19

#
I have this code where everything gets removed, but i need for all except one actor to be removed, how do i do that
1
2
3
4
5
6
7
8
9
10
11
Actor collided = getOneIntersectingObject(End.class);
           if(collided != null)//if you have not run into it
           {
               World world = getWorld();
               world.removeObjects(world.getObjects(null)); //removes all the objects in the world;
               world.addObject(new Congrats(), world.getWidth()/2, world.getHeight()/2);
                
               Greenfoot.stop();
                
               Greenfoot.playSound("Pacman.wav");
           }
jdf1234567 jdf1234567

2015/4/19

#
u can do
1
2
3
4
5
6
7
8
9
10
11
12
Actor collided = getOneIntersectingObject(End.class);
           if(collided != null)
           {
               List actors = getWorld().getObjects().remove(actor);//the one U wanna keep
 
               world.removeObjects(actors);
               world.addObject(new Congrats(), world.getWidth()/2, world.getHeight()/2);
                 
               Greenfoot.stop();
                 
               Greenfoot.playSound("Pacman.wav");
           }
danpost danpost

2015/4/19

#
I will presume that it is the End actor that you wish to have remain in the world; if not, just change 'End.class' in the code below to the proper class of actor you with to retain in the world. Replace line 5 with the following:
1
2
3
java.util.List<Actor> actors = world.getObjects(null);
actors.removeAll(world.getObjects(End.class));
world.removeObjects(actors);
If you have a reference to the one actor you wish to retain in a variable called 'actor', change line 2 here to this:
1
actors.remove(actor);
Douggresham Douggresham

2015/4/19

#
Is the one actor that you are trying to remove unique to its own class? If it is the only actor belonging to a certain class you could try removing all objects of each class one by one like this:
1
world.removeObjects(getObjects(YourClass.class));
If there are other actors you are trying to remove within the class that contains that actor you want to keep you could try:
1
2
3
4
5
6
7
for(Actor x: ListOfActors)
{
   if(x!=myActor)
  {
     world.removeObject(x);
  }
}
jdf1234567 jdf1234567

2015/4/19

#
Douggresham wrote...
Is the one actor that you are trying to remove unique to its own class? If it is the only actor belonging to a certain class you could try removing all objects of each class one by one like this:
1
world.removeObjects(getObjects(YourClass.class));
If there are other actors you are trying to remove within the class that contains that actor you want to keep you could try:
1
2
3
4
5
6
7
for(Actor x: ListOfActors)
{
   if(x!=myActor)
  {
     world.removeObject(x);
  }
}
That's a bit complicated isn't it
danpost danpost

2015/4/19

#
jdf1234567 wrote...
u can do
1
2
3
// LINES OMITTED
List actors = getWorld().getObjects().remove(actor);//the one U wanna keep
// LINES OMITTED
There are a couple of problems with this line. (1) the 'getObjects' method of the World class requires a Class parameter (which could be 'null'); (2) the 'remove' method of the List class used here returns a boolean value -- not the resultant List object; you cannot assign the 'actors' variable which is to hold a List object a boolean value; (3) you are using the List class which requires one of the following line at the beginning of the class:
1
2
3
import java.util.*;
// or
import java.util.List;
jdf1234567 jdf1234567

2015/4/19

#
Sry wasn't really thinking about it; U can use actor.class instead of null Sal well can't u?
jdf1234567 jdf1234567

2015/4/19

#
I hate autocorrect: *as
Super_Hippo Super_Hippo

2015/4/19

#
You can use 'Classname.class' as a parameter for 'getObjects' to get a list of all objects from the class 'Classname'. For your info, you can edit your post as long as no one wrote another message. Click on "Edit" below "Quote" to do so. You don't have to correct yourself in another message. As an alternative, you could also save the actor you want to keep, remove all and re-add the one you wanted to keep.
jdf1234567 jdf1234567

2015/4/19

#
danke für den Hinweis
You need to login to post a reply.