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

2017/5/24

Random spawning

alexaa alexaa

2017/5/24

#
How would I implement a way for an orb to randomly spawn? I typed up this but it spawns one and never does it again randomly each time I reset the game. I was thinking of a while loop where the orbs would stop if the player or boss died but when I tried it never did anything at all.
1
2
3
4
5
6
7
8
public void randomSpawn() {
    if (Greenfoot.getRandomNumber(100) < 60)
    {
         Orb orb = new Orb();
         addObject(orb,526,513);
    }
 
}
danpost danpost

2017/5/24

#
Sixty percent is an awfully large chance to spawn an orb. You will, when you do get it working, probably spawn about 35 to 40 orbs per second. You should be calling the method from the act method of your world class:
1
2
3
4
public void act()
{
    randomSpawn();
}
You were probably calling the method from the world constructor (remove that call).
alexaa alexaa

2017/5/24

#
danpost wrote...
Sixty percent is an awfully large chance to spawn an orb. You will, when you do get it working, probably spawn about 35 to 40 orbs per second. You should be calling the method from the act method of your world class:
1
2
3
4
public void act()
{
    randomSpawn();
}
You were probably calling the method from the world constructor (remove that call).
I was calling it the same place everything else was spawned in the prepare. Isn't prepare the same as act?
danpost danpost

2017/5/24

#
alexaa wrote...
I was calling it the same place everything else was spawned in the prepare. Isn't prepare the same as act?
Everything else (or most everything else) you want initially spawned -- not the orbs. And no, the prepare method is not the same as the act method. The prepare method is (usually) called by the world constructor to fill the world with its initial objects (mainly). It is not normally called to execute again. The act method, on the other hand, is called repeatedly by the greenfoot framework while the scenario is in a running state.
alexaa alexaa

2017/5/24

#
danpost wrote...
alexaa wrote...
I was calling it the same place everything else was spawned in the prepare. Isn't prepare the same as act?
Everything else (or most everything else) you want initially spawned -- not the orbs. And no, the prepare method is not the same as the act method. The prepare method is (usually) called by the world constructor to fill the world with its initial objects (mainly). It is not normally called to execute again. The act method, on the other hand, is called repeatedly by the greenfoot framework while the scenario is in a running state.
Ah, thanks. I'll see what I can do once I get back to my code again. I understand now why it should be called in the act method of my world, i'll test this out!
alexaa alexaa

2017/5/25

#
danpost wrote...
alexaa wrote...
I was calling it the same place everything else was spawned in the prepare. Isn't prepare the same as act?
Everything else (or most everything else) you want initially spawned -- not the orbs. And no, the prepare method is not the same as the act method. The prepare method is (usually) called by the world constructor to fill the world with its initial objects (mainly). It is not normally called to execute again. The act method, on the other hand, is called repeatedly by the greenfoot framework while the scenario is in a running state.
How would i get the orbs to not spawn a ton at a time? I was trying to only get one or two to spawn every couple of seconds. I got it to work but it spawns a ton at a time.
danpost danpost

2017/5/25

#
alexaa wrote...
How would i get the orbs to not spawn a ton at a time? I was trying to only get one or two to spawn every couple of seconds. I got it to work but it spawns a ton at a time.
In this line of the 'randomSpawn' method:
1
if (Greenfoot.getRandomNumber(100) < 60)
either decrease the '60' or increase the '100' (or both).
You need to login to post a reply.