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

2012/12/2

Using getIntersectingObjects()

tayaza tayaza

2012/12/2

#
How do I prevent cars from appearing on top of each other in the simple 2D Car game scenario using the getIntersectingObjects(). I tried "removeObjects()" but it keeps throwing an exception
Kyle273 Kyle273

2012/12/3

#
It'l help a lot if you post which exception you get. For instance, you might need to use getWorld().removeObjects(getIntersectingObjects(Car.class)); since removeObjects() is a world function, not an actor function.
tayaza tayaza

2012/12/3

#
Thanks Kyle273. This is the exception java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. The problem is the other cars that are generated randomly into the world sometimes are on top of each other. I want to avoid that. Tried your code but I still get the same exception. Thanks
danpost danpost

2012/12/3

#
tayaza wrote...
The is the other cars that are generated randomly into the world sometimes are on top of each other.
That may be the problem you are having as far as trying to get the scenario to run in the manner you want; however, it is not why you are getting that message. Like the message says, your actor is NOT in the world; and, therefore, you cannot use any of the 'get...Object...' method calls on that actor. Either you are calling a method in the Car class that contains one of the 'get...Object...' method calls in it on a referenced Car object that is not in the world; or, you had previously, within the same 'act' cycle, removed 'this' Car object from the world and are continuing to do unneccessary evaluations on 'this' object whose code contains one of those method calls. Chances are it is the latter case; in which case you need to add the following line somewhere in your Car class code (before the 'get...Object...' call that is causing the problem:
if(getWorld()==null) return;
danpost danpost

2012/12/3

#
You need first to work on getting the code to run without any errors. Then, one thing at a time, get it to do (or not do) those things you want. Get each thing working before moving on to the next.
legoman legoman

2012/12/3

#
You also wouldn't want to use
getWorld().removeObjects(getIntersectingObjects(Car.class));
verbatim, as it'll return null if there are no intersecting objects, throwing an error. Also where is your check? If you check inside the constructor, the actor hasn't been added to the world yet.
danpost danpost

2012/12/3

#
legoman wrote...
You also wouldn't want to use
getWorld().removeObjects(getIntersectingObjects(Car.class));
verbatim, as it'll return null if there are no intersecting objects, throwing an error.
'getIntersectingObjects' will always return a List object. If there are none to list, it returns an empty list; but, it will never return 'null'.
tayaza tayaza

2012/12/3

#
Okay guys. Will try your tips. Thanks
tayaza tayaza

2012/12/3

#
if(getWorld()==null) return;  
Yes, it works! I appreciate.
legoman legoman

2012/12/4

#
danpost wrote...
'getIntersectingObjects' will always return a List object. If there are none to list, it returns an empty list; but, it will never return 'null'.
my bad. I'm much more used to getOneIntersectingObject. I always checked anyways with getIntersectingObjects, for good measure at the least.
You need to login to post a reply.