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

2013/10/8

Removing Object from world

Cyan002 Cyan002

2013/10/8

#
Hi guys, I've been trying to get this to work for a while but just cant seem to get everything in place. Im trying to remove a plane from the world once the score hits 50 to allow for a sort of boss to appear. So far all I have is
public class PlaneWorld extends World
{
  public void bossSpawn()
    {
        if(score == 50)
        {
            World planeWorld = (Airplane) getObjects();
            planeWorld.removeObject(Airplane.class);
            
        }
    }
}
any help would be greatly appreciated.
JetLennit JetLennit

2013/10/8

#
I think this is what you were asking.... Try changing the code to
public class PlaneWorld extends World  
{  
  public void bossSpawn()  
    {  
        if(score == 50)  
        {  
            Airplane a = new Airplane()
            getWorld().removeObject(a);  
        }  
    }  
}  
Cyan002 Cyan002

2013/10/8

#
No I already have an existing Airplane in the world. So Im trying to remove that once the score hits 50.
danpost danpost

2013/10/8

#
@JetLennit, that will not remove the airplane that is in the world from the world. You are creating a new instance of Airplane (a different one than that which is already in the world) and trying to remove it from the world (lines 7 and 8). Also 'getWorld' is an Actor class method and will error when used in a World subclass. @Cyan002, line 7 of your code seems all out of sorts. It looks like you are getting a list of objects without supplying a class as an argument, getObjects(???ClassName.class???), then trying to typecase it to be an Airplane object '(Airplane)'; next, you are trying to assign that ?list? to a field called 'planeworld' that is to hold a World object. On line 8, you are telling the world (at least, that is what it should be) to remove a Class from the world , not an object. OK: let us walk through this: You are in a World subclass and need to remove an Airplane object from that world. You can get a list of all Airplane objects that are in the world by using the 'getObjects' method. This method needs a class (or null, for all classes) passed to it, so it knows what to put into the list that is returned. This would look like this 'getObjects(Airplane.class)'. There is a World class method 'removeObjects' (notice this one is plural) that will remove a list of objects from the world. Again, we must pass something to this method as well; the list of Airplane objects just returned from 'getObjects(Airplane.class)'. We can pass this list just as it is; like so 'removeObjects(getObjects(Airplane.class));'.
Cyan002 Cyan002

2013/10/8

#
So because its already within the World Class I don't need to call the world to find the plane and remove it. I'm sort of getting it. And the getObjects and removeObjects creates lists in which the object in the brackets are the list. I just want to make sure I'm understanding all of this correctly.
danpost danpost

2013/10/8

#
Anytime you run a non-static method (static methods would have the word 'static' in the method descriptor line; do not worry as to what they are right now), but anytime you run a non-static method, there is an object involved that the method is being run for or on. For example: your 'bossSpawn' method is a method in your World sub-class called PlaneWorld. When you reset this project, a new instance of PlaneWorld (a World object) is created. To execute the 'bossSpawn' method from the PlaneWorld class, you can just say 'bossSpawn;' which is equivalent to 'this.bossSpawn();'. The 'this' keyword refers to the current world and the method is run on or for that world. To execute the method from and Actor subclass, you would need to get a reference to the world and typecast it to PlaneWorld, '((PlaneWorld)getWorld()).bossSpawn();', because that is where the method is located. Some methods require additional information to run. The 'getObjects' method require a Class object (or 'null') so it knows what objects to add to the list that is returned. The 'removeObjects' method requires a list of Object so it knows what objects to remove from the world. The 'removeObject' (notice, this is singular) method requires a single Actor object (not a list) so it knows what object to remove. Review the online documentation of the Greenfoot API to see what methods there are, what arguments are required and what type of object they return, if any. The 'Documentation' link on the top will sent you to a page that has a link to the online API.
matheusdcb matheusdcb

2013/10/9

#
If you're already in object the be removed, when is only of removeObject(this);
You need to login to post a reply.