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

2015/10/26

Limiting enemy spawns

Larez Larez

2015/10/26

#
Can you guys help me in limiting the spawn of the enemy? TIA
danpost danpost

2015/10/26

#
Larez wrote...
Can you guys help me in limiting the spawn of the enemy? TIA
What code are you using to spawn? what limitation(s) do you wish to impart?
Larez Larez

2015/10/26

#
1
2
3
4
5
6
7
public void spawns()
{
if(Greenfoot.getRandomNumber (50) < 1)
{
addObject(new Asteroid1(), 599, Greenfoot.getRandomNumber(400));
}
}
This is the code That I'm currently using. By limitations, I want to limit the spawn of the enemy. Ex: Only 30 asteroids in the first wave.
Super_Hippo Super_Hippo

2015/10/26

#
If you want exactly 30 asteriods, change line 3 to this:
1
for (int i=0; i<30; i++)
If you want a maximum of 30, you can add the line before line 3. (In this case, you should change the 2% you are using to something higher.)
danpost danpost

2015/10/26

#
Larez wrote...
< Code Omitted > This is the code That I'm currently using. By limitations, I want to limit the spawn of the enemy. Ex: Only 30 asteroids in the first wave.
First thing, if you want the value of the limit to be variable, you will need to use a field for its value. As such, you would have this:
1
2
3
4
5
6
7
8
9
10
11
// instance field
private int toSpawn = 30; // setting initial value
// the code
public void spawns()
{
    if (toSpawn>0 && Greenfoot.getRandomNumber(50)<1)
    {
        addObject(new Asteroid1(), 599, Greenfoot.getRandomNumber(400));
        toSpawn--; //
    }
}
You can then use some expression that depends on the wave number to set the number of enemies to produce each wave. Something like:
1
toSpawn = 25+5*waveNumber;
Larez Larez

2015/10/26

#
Finally! Thank you for the help. :D
You need to login to post a reply.