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

2018/11/2

Is there any way to spawn ememies at an interval?

CharBun CharBun

2018/11/2

#
For class I'm making a sort of tower defence where I want enemies to come in waves that take about 45 seconds (or maybe just 15 tanks ) and then have a resting period of 15 seconds if you defeat them all. I'm new to all this and I am mildy overwhelmed and I can't figure out how I should do this. Anyone got some advice?
danpost danpost

2018/11/2

#
CharBun wrote...
For class I'm making a sort of tower defence where I want enemies to come in waves that take about 45 seconds (or maybe just 15 tanks ) and then have a resting period of 15 seconds if you defeat them all. ... Anyone got some advice?
Use an int field to count act cycles. Start its value at 60 (60 acts/sec) times 15 (secs to rest) plus 60 (60 act/sec) times # of tanks times # secs between spawning of tanks within the same wave. This will be the total number of acts for spawning and resting. Only other time may be between when last tank in wave is spawned to when no tanks remain in world. The number of tanks as well as the rate at which they spawn can be made variable. The counter would be controlled similar to that given below:
// fields (variables)
int tankCount;
int spawnRate; // secs between spawn during a wave
int pauseTime = 15; // time between waves in secs
int actCount;

// initializing a wave
tankCount = 10;
spawnRate = 3;
actCount = 60*(pauseTime+tankCount*spawnRate);

// in act
if (actCount != 60*pauseTime) // spawning or resting
{
    if (--actCount == 0) newWave(); // end resting
    else if (actCount >= 60*pauseTime && (actCount-60*pauseTime)%(60*spawnRate) == 0) spawnTank();
}
else if (getObjects(Tank.class).isEmpty()) actCount--; // start resting
CharBun CharBun

2018/11/2

#
danpost wrote...
CharBun wrote...
For class I'm making a sort of tower defence where I want enemies to come in waves that take about 45 seconds (or maybe just 15 tanks ) and then have a resting period of 15 seconds if you defeat them all. ... Anyone got some advice?
Use an int field to count act cycles. Start its value at 60 (60 acts/sec) times 15 (secs to rest) plus 60 (60 act/sec) times # of tanks times # secs between spawning of tanks within the same wave. This will be the total number of acts for spawning and resting. Only other time may be between when last tank in wave is spawned to when no tanks remain in world. The number of tanks as well as the rate at which they spawn can be made variable. The counter would be controlled similar to that given below:
// fields (variables)
int tankCount;
int spawnRate; // secs between spawn during a wave
int pauseTime = 15; // time between waves in secs
int actCount;

// initializing a wave
tankCount = 10;
spawnRate = 3;
actCount = 60*(pauseTime+tankCount*spawnRate);

// in act
if (actCount != 60*pauseTime) // spawning or resting
{
    if (--actCount == 0) newWave(); // end resting
    else if (actCount >= 60*pauseTime && (actCount-60*pauseTime)%(60*spawnRate) == 0) spawnTank();
}
else if (getObjects(Tank.class).isEmpty()) actCount--; // start resting
I have most code in place right now, but I really don't understand what I should put in with the newWave() method? Could you please help me a little bit? (also thank you, many of your responses on other threads have helped me so so much!)
danpost danpost

2018/11/2

#
CharBun wrote...
I have most code in place right now, but I really don't understand what I should put in with the newWave() method?
Other than making sure all unwanted actors are removed from the world (which I cannot really think of any, for a tower defense game), its just adjusting your variables (maybe increasing the wave count, increasing the number of enemies and resetting the act counter).
CharBun CharBun

2018/11/2

#
danpost wrote...
Other than making sure all unwanted actors.....
    int tankCount;
    int spawnRate; //spawning tanks
    int interval = 15; //time between waves
    int actCount;
   
    public void act()
    {
      //new wave
      actCount = 60*(interval+tankCount*spawnRate);
      tankCount = 10;
      spawnRate = 3;
      
      if (actCount != 60*interval) // spawning or resting
      {
            if (--actCount == 0)
            {
             newWave(); // end resting
            } 
            else if (actCount >= 60*interval && (actCount-60*interval)%(60*spawnRate) == 0) 
            {
              spawnTank();
            }
       }
      else if (getObjects(tank.class).isEmpty())
       {
           actCount--; // start resting
       }
    }
   
    public void newWave()
    {
    }
    
        public void spawnTank()
    {
    
        addObject(new tank(),681, 756);
     
    }
I have this right but it doesn't work?
danpost danpost

2018/11/2

#
CharBun wrote...
<< Code Omitted >> I have this right but it doesn't work?
Lines 10 and 11 should go in a constructor and line 9 should go in the newWave method. Remove lines 9 through 11 and add/edit the following blocks of code:
public MyWorld()
{
    super(/** whatever */);
    // the following lines go here
    tankCount = 10;
    spawnRate = 3;
    newWave();
}

// the newWave method can just be this
public void newWave()
{
    actCount = 60*(interval+tankCount*spawnRate);
}
You need to login to post a reply.