Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  firing timer  (Read 980 times)
0 Members and 1 Guest are viewing this topic.
Offline javatypo

Full Member
**

Posts: 143



« on: 2005-03-09 14:07:15 »

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
Offline Vorax

JGO Ninja
***

Posts: 666
Medals: 1


System shutting down in 5..4..3...


« Reply #1 on: 2005-03-09 15:43:22 »

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.

Offline javatypo

Full Member
**

Posts: 143



« Reply #2 on: 2005-03-12 20:19:41 »

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

thanks Cheesy
Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.093 seconds with 22 queries.