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

2013/1/11

How to make worms appear at random locations

Peach Peach

2013/1/11

#
Hi so far I have got this code and I would like to make RedWorms appear at random locations after the counter value is 10. How can I do that? because now, after the counter value becomes 10, only 1 RedWorm appears. What is wrong with my code? public void lookForWorm() { if (canSee(Worm.class)) { eat(Worm.class); counter .add(1); if (counter.getValue() == 10) { getWorld().addObject(new RedWorm(), Greenfoot.getRandomNumber(950), Greenfoot.getRandomNumber(550));
danpost danpost

2013/1/11

#
Instead of '==', use '>='.
Peach Peach

2013/1/11

#
Now every time I eat a red worm, another one appears at a different location. It is possible to make them appear at random locations, 2% of the time, with a max of 20 red worms? thanks
danpost danpost

2013/1/11

#
Just add the following conditions on creating the RedWorm object: (1) Greenfoot.getRandomNumber(100)<2, and (2) getWorld().getObjects(RedWorm.class).size()<20)
Peach Peach

2013/1/11

#
it says that it is not a statement
danpost danpost

2013/1/11

#
It says what is not a statement (show what you have).
Peach Peach

2013/1/13

#
it says that Greenfoot.getRandomNumber(100)<2 is not a statement
danpost danpost

2013/1/13

#
I did say that they were conditions (for an 'if' statement).
You need to login to post a reply.