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

2014/10/25

remove objects with a specified parameter

Duck_of_doom Duck_of_doom

2014/10/25

#
Hello! I wonder if it's possible to remove a group of objects with for example a string as given parameter. Say that we have a class Robot.class and that the robot class has different Images eg red blue and black. If I have several of objects of the robot class placed in my world how do I remove all with a specified color? In this case it's not desirable to split this class into several classes with the respective color. Hope someone can help me out with this one!
Super_Hippo Super_Hippo

2014/10/25

#
for (Object obj : getWorld().getObjects(Robot.class))
{
    Robot r = (Robot) obj;
    if (r.image == 2) getWorld().removeObject(r);
}
In this case, I used 'image' as in int. So if the image changes, then you change this variable. For example 1 is red, 2 is blue and 3 is black. If you have this variable private, then you need a getter-method for it. Maybe you could also use this, but I am not sure about that:
for (Actor r : getWorld().getObjects(Robot.class))
{
    if (r.getImage() == new GreenfootImage("blue.png"))
    /*better save this image somewhere, for example in your world as public static and
    call Worldname.imageBlue then*/
    getWorld().removeObject(r);
}
davmac davmac

2014/10/25

#
Maybe you could also use this, ...
    if (r.getImage() == new GreenfootImage("blue.png"))  
No!!! a pre-existing object can never be the same object as one you just created. Perhaps you meant:
    if (r.getImage().equals(new GreenfootImage("blue.png")))
However, I would not rely on this working either. The "equals" method for GreenfootImage is not well defined.
Super_Hippo Super_Hippo

2014/10/25

#
But it would work if you save this image as a field for example in the world and compare it then, right? That's how I am creating images, so not every object creates its own image. This way the image should be the same object. At least I would think this way.
public static GreenfootImage[] img_rob;

public Worldname()
{
    super(/*...*/);
    createImages();
}

public void createImages()
{
    img_rob = new GreenfootImage[3];
    for (int i=0; i<=2; i++)
    {
        img_rob[i] = new GreenfootImage("robot"+i+".png");
    }
}
public Robot()
{
    setImage(Worldname.img_rob[0];
}
for (Actor r : getWorld().getObjects(Robot.class))
{
    if (r.getImage() == Worldname.img_rob[1]) getWorld().removeObject(r);
}
davmac davmac

2014/10/25

#
But it would work if you save this image as a field for example in the world and compare it then, right?
Yes, that would work :)
Duck_of_doom Duck_of_doom

2014/10/25

#
for (Actor r : getWorld().getObjects(Robot.class))  
{  
    if (r.getImage() == Worldname.img_rob[1]) getWorld().removeObject(r);  
}
This code don't work, it gives me an error saying "Incompatible types".
danpost danpost

2014/10/25

#
Cannot assign elements of the list created by 'getObjects' to a variable that holds an Actor without first casting the element to the Actor class. Try this:
for (Object obj : getWorld().getObjects(Robot.class))  
{
    Actor r = (Actor)obj;
    if (r.getImage() == Worldname.img_rob[1]) getWorld().removeObject(r);  
}
Here, line 3 typecasts the object element returned from within the list to an Actor and assign it to an Actor variable. Notice that the elements are typed as Object objects within the list itself and that is what they are extracted as.
You need to login to post a reply.