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

2012/12/14

How do you generate objects in random spots until a game ends?

abarnes abarnes

2012/12/14

#
Hey, I'm trying to create a game and what I'm ultimately stuck on is having a certain object (fish in my case) keep spawning (let's say 3 at a time) until the game ends. any help would be appreciated, thank you.
danpost danpost

2012/12/14

#
What will be the trigger to add each group of fish?
davemib123 davemib123

2012/12/14

#
abarnes abarnes

2012/12/14

#
I guess I'll just make the trigger how it is in that link, I'm new to this so I wouldn't know how to do it on a timer or anything like that haha. Thanks!
abarnes abarnes

2012/12/14

#
Wait the code in that link isn't working. Whenever I eat the 5 fish, it doesn't add any new ones.
davemib123 davemib123

2012/12/14

#
you tried the code that danpost has posted in the second message on that link?
abarnes abarnes

2012/12/14

#
Yes public void moreFood() { while (getObjects(Fishfood.class).size() < 5){ addObject(new Fishfood(), Greenfoot.getRandomNumber(600), Greenfoot.getRandomNumber(400)); } }
danpost danpost

2012/12/14

#
Did you add a call to this method in the 'act' method?
abarnes abarnes

2012/12/14

#
public class FishWorld extends World { /** * Constructor for objects of class FishWorld. * */ public FishWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); populateWorld(); moreFood(); } public void populateWorld() { addObject(new Fishy(), 200,100); addObject(new Shark(), 80, Greenfoot.getRandomNumber(300)); } public void moreFood() { while ((getObjects(Fishfood.class).size ()< 3)) addObject(new Fishfood(), 800, Greenfoot.getRandomNumber(300)); } }
abarnes abarnes

2012/12/14

#
D:
danpost danpost

2012/12/14

#
Having 'moreFood' in the constructor of the world will put the initial food in the world. However, you need to add an 'act' method and call it from there also, so the food count will remain at three.
abarnes abarnes

2012/12/14

#
Oh that did it haha. Thanks!!
abarnes abarnes

2012/12/14

#
Now I'm stuck on a scoreboard >.>
You need to login to post a reply.