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

2017/7/13

Adding Enemies in different waves

LobsterL LobsterL

2017/7/13

#
I want to have my world add 50 enemies in one wave but have two enemies spawn every 2 seconds and once they are killed have the new wave start. can anyone help me?
Super_Hippo Super_Hippo

2017/7/13

#
To add 50 enemies, you can use a for loop:
public void newWave()
{
    for (int i=0; i<50; i++)
    {
        addObject(new Enemy(), /*x-position*/, /*y-position*/);
    }
}
To add two enemies every two seconds, you can do this:
private int enemySpawnTimer = -1;

public void act()
{
    if (++enemySpawnTimer == 55*2)
    {
        enemySpawnTimer = 0;
        for (int i=0; i<2; i++) addObject(new Enemy(), /*x-position*/, /*y-position*/);
    }
}
I am not too sure what you meant with the last part, the wave trigger. Two new enemies are spawned every two seconds, should a new wave start whenever one of those enemy pairs are killed or whenever there is no enemy object in the world?
LobsterL LobsterL

2017/7/14

#
What i mean is that when fifty enemies are spawned. then the spawning stops and once all of them are killed a new wave starts
Super_Hippo Super_Hippo

2017/7/14

#
So you mean the wave is 25*2 enemies?
private int enemySpawnTimer = 0;
private int spawnEnemies = 0;

public void act()
{
    if (spawnEnemies > 0)
    {
        if (++enemySpawnTimer == 55*2)
        {
            enemySpawnTimer = 0;
            spawnEnemies -= 2;
            for (int i=0; i<2; i++) addObject(new Enemy(), /*x-position*/, /*y-position*/);
        }
    }
    else //no enemies will spawn in this wave anymore
    {
        if (getObject(Enemy.class).isEmpty()) //no enemies in the world
        {
            spawnEnemies = 2*25;
        }
    }   
}
LobsterL LobsterL

2017/7/16

#
yes and when all the enemies are killed the wave ends and a new wave starts
Super_Hippo Super_Hippo

2017/7/16

#
Lines 15 through 21 should handle that. You have to change 'getObject' to 'getObjects' in line 17.
LobsterL LobsterL

2017/7/17

#
Thanks
LobsterL LobsterL

2017/7/17

#
I'm getting an error with line 12 and even when putting in getObjects and error still occurs
Super_Hippo Super_Hippo

2017/7/17

#
What is the error message? Did you put in values for x and y?
LobsterL LobsterL

2017/7/17

#
i have put in the x and y coordinates and the error says that it cannot find symbol - method addobject(Enemy,int,int) and the it is the same for the get objects method (java.lang.Class<Enemy>)
danpost danpost

2017/7/17

#
LobsterL wrote...
i have put in the x and y coordinates and the error says that it cannot find symbol - method addobject(Enemy,int,int) and the it is the same for the get objects method (java.lang.Class<Enemy>)
It sounds like you did not put said code into your subclass of World where it belongs.
LobsterL LobsterL

2017/7/17

#
Can you show me what that code would look like? sorry i'm a beginner
danpost danpost

2017/7/18

#
LobsterL wrote...
Can you show me what that code would look like?
Hippo gave you the code three days ago -- right here.
LobsterL LobsterL

2017/7/18

#
i have figured it out thanks for the help
You need to login to post a reply.