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

2011/5/2

Help with this scenario!

blakeesh blakeesh

2011/5/2

#
I'm working on a scenario similar to the ants scenario from chapter 9 in the Intro to programming book... In this scenario there are ants coming out of an anthill, and anteaters coming out of a cave. The anteaters wonder around the world and if they come in conctact with an ant, it is suppose to "eat" it, and thus the ant is removed from the world... In the anteater class code, I had the method where it checks for intersecting objects (ant.class) and if !=null, removeObject(this). However it doesn't compile... I've tried replacing this with ant, Ant, Ant.class... none seem to work, the error message is "cant find variable removeObject". Why is this? any tips? THANKS!
davmac davmac

2011/5/2

#
Post your code! At least the line where you get the error.
blakeesh blakeesh

2011/5/2

#
Well I don't have it with me right now on my computer, it's on the servers at the lab at my school... But it looks something like this(from my memory)... In the Anteater Class: Private void checkForAnts() { getIntersetctingObjects(Ants.class); if ( !=null); { removeObject(this) } }
blakeesh blakeesh

2011/5/2

#
I'm gonna go back tonight and try to work on it some more... I'll post the exact code then.
davmac davmac

2011/5/2

#
Yes, we need the exact code to be able to help.
Builderboy2005 Builderboy2005

2011/5/2

#
I would suggest trying getWorld().removeObject(blah blah blah) instead of removeObject(blah blah blah)
blakeesh blakeesh

2011/5/2

#
Hmm.. Last night I tried in calling getWorld(); in the checkForAnts() Method, before I called removeObject(); However I didn't try getWorld().removeObject(...). Is there a difference?
Builderboy2005 Builderboy2005

2011/5/2

#
Yes, a very big difference. By saying
getWorld()
removeObjects()
you are saying "Get the world! do nothing with it. And then try to removeObjects" But only the world is able to remove objects, so you need to do this:
getWorld().removeObjects()
which says "Get the world, and then *use* the world to remove this object" which is what you want
You need to login to post a reply.