# Timers and enemy spawning

This topic is 4675 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a question that's not really specific to any API or platform, just...game development. I'm making a Space Invaders type thing, and I've got different types of enemies (they move at different speeds, have corresponding point values, and so on). My question is, how can I decently handle the spawning of these enemies? Right now I have two enemies and two timers, one for each type of enemy. I'm sure there is some smarter way of doing this. Here's a bit of the code I have:
    if(timer1>=4000)
{
enemy[0].alive = true;
enemy[0].x = -enemy[0].width;
enemy[0].y = 15;
timer1 = 0;
}
if(timer2>=6000)
{
enemy[1].alive = true;
enemy[1].x = -enemy[1].width;
enemy[1].y = 10;
timer2=0;
}
...
timer1 += DELAY;
timer2 += DELAY;


So it spawns an enemy[0] every 4 seconds and an enemy[1] every 6 seconds. But is there a less clunky way of doing this than what I've got here? (The DELAY is rest(20) if that matters) What do people normally do for this type of stuff?

##### Share on other sites
This is certainly not perfect, or perhaps even good, but I don't need timers much and it works:

in main game loop
        timediff=(GetTickCount()-timeptr)/33;        r=r+(GetTickCount()-timeptr)%33;        if (r>33){                r=r-33;                timediff++;        }        if (focused){                for (v=timers;v;v=v->next){                        timetmp=(long *)v->data;                        *timetmp=*timetmp-timediff;                }        }

in secondary file
class   ro_counter{private:protected:        ro_counter(long inc=0){counter=inc;(new vine(TIMER,&counter))->top(&timers);}public:        long    counter;        virtual         ~ro_counter(){                timers->retr(&counter)->remove(&timers);        }};

Pardon my use of personal lists [and globals!] but the essense holds. Any class that needs a counter, simply inherits from the ro_counter class. The main game loop then updates the counters all together. The classes themselves can then do whatever they'd like with the counter in their own code [usually in ->update() or ->render()]