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

2023/4/29

Limit the Spawner

Mzarifin58 Mzarifin58

2023/4/29

#
I want to make only 20 spawn enemies, but i want there is a time delay in each spawn. What program should I make for this problem?
danpost danpost

2023/4/29

#
Mzarifin58 wrote...
I want to make only 20 spawn enemies, but i want there is a time delay in each spawn. What program should I make for this problem?
The following should work:
private int enemyCount = 20;
private int enemySpawnTimer = -1;
private int enemySpawnDelay = 180; // approx. 3 second time gap between spawns (currently constant)

public void act() {
    if (enemyCount > 0) {
        enemySpawnTimer++;
        if (enemySpawnTimer%enemySpawnDelay == 0) {
            int x = 0; // make whatever x-coordinate you wish to spawn at
            int y = 0; // make whatever y-coordinate you wish to spawn at
            addObject(new Enemy(), x, y);
            enemyCount--;
        }
    }
}
Mzarifin58 Mzarifin58

2023/4/30

#
I have try your program, but why does the enemy spawn all at once?
Mzarifin58 Mzarifin58

2023/4/30

#
And them spawn continuosly
danpost danpost

2023/4/30

#
Mzarifin58 wrote...
I have try your program, but why does the enemy spawn all at once? And them spawn continuosly
They should not. Please show your code (entire class please).
You need to login to post a reply.