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

2013/2/18

removeObject(s) method

1
2
Gingervitis Gingervitis

2013/2/18

#
How do you use the removeObject and removeObjects method? I understand it will remove all the objects you want it to, but how do you set up the parameters?
danpost danpost

2013/2/18

#
For the 'removeObject' method: supply a reference to an Actor object (or call a method that returns an Actor object). For the 'removeObjects' method: supply a list of Actor objects. Most commonly, a method is called to return the list of Actor objects (the World class methods 'getObjects(Class cls)' and 'getObjectsAt(int x, int y, Class cls)' and the Actor class methods 'getIntersectingObjects(Class cls)', 'getNeighbours(Class cls)', 'getObjectsAtOffset(int dx, int dy, Class cls) and 'getObjectsInRange(int dist, boolean diagonal, Class cls)' all return lists of actors. By supplying 'null' for the 'Class cls' parameter, ALL actors found (regardless of class) will be returned in the list.
Gingervitis Gingervitis

2013/2/18

#
I just tested out what you said about putting 'null' for the 'Class cls' parameter, and I got a null pointer exception. I don't need it in any of my current code, but why did that happen?
danpost danpost

2013/2/18

#
You probably tested it in 'removeObject' instead of 'removeObjects'. When I mentioned using 'null', I did say a list would be returned; meaning you can use it in 'removeObjects' (not 'removeObject').
Gingervitis Gingervitis

2013/2/18

#
I did test it in the 'removeObjects' method when I got the null pointer exception. I re-tested it in another spot in my 'Turtle Adventure' game and there was no null pointer exception, but nothing happened.
danpost danpost

2013/2/18

#
Oooh, sorry. You cannot use null in this location. What I meant was removeObjects(getObjects(null)); 'getObjects' can have null as the parameter value, not any of the methods that remove objects.
Gingervitis Gingervitis

2013/2/18

#
Ohhh, that makes perfect sense! You want to remove all the objects so you have to get all the objects first.
Gingervitis Gingervitis

2013/2/18

#
I put this code in 'world.removeObjects(getObjects(null));' but it said "cannot find symbol - method getObjects(<nulltype>)"
Gevater_Tod4711 Gevater_Tod4711

2013/2/18

#
It has to be world.removeObjects(world.getObjects(null)); getObjects() is a method in world so you need the reference to the world to call the method.
Gingervitis Gingervitis

2013/2/18

#
ohhh ok. Thank you. Will it be common that once that statement executes, the "Actor not in world" error will appear?
Gevater_Tod4711 Gevater_Tod4711

2013/2/18

#
That should not happen because of this line. Could there be another line that causes the errer? It probably is a method like getWorld() that you call after removing the object from the world.
danpost danpost

2013/2/18

#
Yes, all Actor object contain an internal reference to the world that the actor is in. If the actor is not in any world, the reference holds the value of 'null'. The 'getWorld' method will return whatever value that this field holds. If this field does hold 'null', any action that require getting the location of the actor in a world will produce that error. The moment you execute a 'removeObect' statement that removes the actor currently being acted on from the world, that actor's world field will hold 'null'.
Gingervitis Gingervitis

2013/2/18

#
How could I make the 'removeObjects' method work with out getting the "Actor not in world" error. When I start Turtle Adventure 2, I want to use that method to remove all the objects and then create a new set of actors in one world, instead of making more than 20 worlds.
Gevater_Tod4711 Gevater_Tod4711

2013/2/18

#
If the removeObjects method call is the last one in the method there should not be any errors. Or you could add a return; after this line. Then the method will break up and will not execute any more lines that could cause errors.
Gingervitis Gingervitis

2013/2/18

#
It is the last in the method... Would it be easier to supply a list of actors to remove and then just set the location of the actor you control to a different location?
There are more replies on the next page.
1
2