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

2013/10/3

Regenerating worms

tmnsquirtles tmnsquirtles

2013/10/3

#
For my computer programming class I am required to alter the little crab scenario to make it so when a worm is eaten, a new one appears at a random location and I do not know how I would do that. Any advice?
bourne bourne

2013/10/3

#
When a worm is "eaten", just "teleport" it to new location
danpost danpost

2013/10/4

#
If the assignment says that a 'new one appears at a random location', then I would not suggest just "teleporting" it. In fact, if the Animal class is being used as a superclass for the crab, then calling 'eat' would remove the worm without getting a reference to it. You probably have this so far:
if (canSee(Worm.class))
{
    eat(Worm.class);
}
The 'if' block (between '{' and '}') is where (or when) the worm is eaten. Add a statement (or a combination of statements) within this block of code to add a new worm into the world at a random location. Looking at the emphasized words, you should be able to figure out what methods you would need. First, you are in an Actor subclass and need a world object to add the object into. After getting the world object, you need to create a new worm object and get random x and y coordinates for adding it into the world at. Finally, have that world object add that new worm object at those random coordinates.
You need to login to post a reply.