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

2018/1/16

Please help!

agray11202 agray11202

2018/1/16

#
So I'm planning to use the Greenfoot.getRandomNumber() method to randomly spawn different actors into my world at random times in random places. How would I go about doing this???
danpost danpost

2018/1/16

#
agray11202 wrote...
So I'm planning to use the Greenfoot.getRandomNumber() method to randomly spawn different actors into my world at random times in random places. How would I go about doing this???
Use the act method of your World subclass to perform those duties. You can either use straight random chance or set a random amount of time before a spawning (which gives more control over how often or soon after one spawning that the next occurs).
xbLank xbLank

2018/1/16

#
Snippet of my game:
public class MyWorld extends World
{
	private int spawnTime = 50,
                wait = 0;
    public void act()
    {
	    if(wait == 0)
                RandomSpawn();
            else wait--;
    }
    private void RandomSpawn()
    {
        addObject(new Mob(GetSpeed()),999,getRandomNumber(35,765));
        wait = spawnTime;
    }
    private int getRandomNumber(int start, int range)
    {
        return start + (int)Greenfoot.getRandomNumber(range-start+1);
    }
}
agray11202 agray11202

2018/1/16

#
But how would I set up the code?
agray11202 agray11202

2018/1/16

#
oh thank you
xbLank xbLank

2018/1/16

#
Line 13: 35 and 765 is refering to the range in which the mobs should spawn (on the y axis) in the world (my world is 1000x800 cells big). 999 (on the y axis) basically means that they should spawn on the very right end of my world.
You need to login to post a reply.