firing timer

hey everyone, what im asking is fairly basic, but anyway here goes. in some of the space shooter games i made u always had to continually press the space bar to fire, but i want to make it so that u can press and hold the space bar, and it will fire bullets at a constant rate, but not so that it shoots them out really fast.

so what im asking is how would one go about coding up some sort of timer or something to do this? i usually just make a variable that counts miliseconds and then when i get a certain amount i fire the next shot, or do the next thing. but i was thinking that there must be a better or different way to do this.

thanks

Set a time ahead variable something like:

private long nextShotTime = 0;

private static final int SHOT_DELAY = 100;

private void shoot() {

long currentTime = System.currentTimeMilleseconds();

if (currentTime >= nextShotTime) {
… fire your weapon here …

nextShotTime = currentTime + SHOT_DELAY;

}
}

That’s about it.

hmm cool. thats what i did for something else, i forget what… but it was the same type of code.

thanks :smiley: