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

2016/5/9

Slowly Spawning Objects

Validity Validity

2016/5/9

#
Hello, I am attempting to create a game where objects (food specifically) are falling down from the top edge. Calling the addObject() method from the constructor will only create the objects once (from what I learned); however, I want to continually spawning objects from the top slowly as the game progresses. I have tried placing the code in the act() method, but the objects were spawning really fast like a thunderstorm, which was not my intention. So far, I have a spawnFood() method to spawn the food, and it is placed inside the act() method.
if(Greenfoot.getRandomNumber(15) < 6)
            {
                addObject(new Food1(), Greenfoot.getRandomNumber(1000), 2);
            }
if(Greenfoot.getRandomNumber(15) < 6)
            {
                addObject(new Food(), Greenfoot.getRandomNumber(1000), 2);
            }
//and so forth
By the way, if I need further help for this game, should I expand this discussion even if the coding topic is different, or should I make another discussion?
danpost danpost

2016/5/9

#
If you change the '15's to larger values and/or the '6's to smaller values then that will reduce the rate of spawning. Removing one of the 'if' blocks (since they are both the same) will also cut the spawning rate in half (and reduce your code). To be sure, the act() method is the proper place for this spawning code (or a method that is called by the act() method). Different topics of discussion deserve to have a heading (or title) of their own (even when referring to the same scenario).
You need to login to post a reply.