Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / delay between bullets fired?

Author
Message
Tdawg
16
Years of Service
User Offline
Joined: 24th Nov 2007
Location:
Posted: 4th Apr 2008 01:00
hi,

Im making a simple 2d shooter, and I was wondering whats the best method for making a delay in between bullets fired.

Right now if I hold down the space bar there is just a continous stream of bullets until you run out. How would I go about making it so I would have to release spacebar and then press it back down again before another bullet would be fired? dbSleep() or dbWait() don't work obviously, they just slow down the whole game.

any ideas would be appreciated! I can upload the code for my Ship class if you want
Codger
21
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 4th Apr 2008 03:22
add a bullet timer when you fire a bullet set the timer to timer() +500

All that is left is to check if the timer is greater than the bullet timer and the spacebar has been pressed

Codger

System
PIV 2.8 MZ 512 Mem
FX 5600 256 mem
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 4th Apr 2008 08:00
Quote: "How would I go about making it so I would have to release spacebar and then press it back down again before another bullet would be fired? dbSleep() or dbWait() don't work obviously, they just slow down the whole game."
I worked out something like this for the mouse by keeping a boolean when it's clicked and not recognizing it as down until it was released again at which point I reset the boolean. Unfortunately this also requires that the status of the mouse be tested frequently. If it's not tested when the mouse is release and the mouse is clicked again it doesn't know about the change. Your keyboard code should be able to do this.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 8th Apr 2008 03:11 Edited at: 8th Apr 2008 03:14
jgc_timer.h - Modified fo stand alone usage


jgc_timer.cpp - again - modified for standalone usage


See if that is useful to you.

Example:


Hope between all these posts you can get things working.

[edit]
That example I posted will work, but note that if you set the TIMER before the loop, you can just test the if delay passed thing - because if you look at the code, you'll notice every time you check to see if enough time has elapsed, this class resets itself to go again using the same interval and even takes any extra time and applies it correctly so you can get a machine gun effect without needing to set the timer each go around.
[/edit]

Codger
21
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 8th Apr 2008 03:51
Jason
Should this be outside the while loop?


Quote: " MyClock=dbTimer();"


Codger

System
PIV 2.8 MZ 512 Mem
FX 5600 256 mem
jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 8th Apr 2008 03:56
No. Then the variable "MyClock" won't always have the current time - which the Timer class I posted uses as a parameter. That's how it stays "Aware" of time passing.

Codger
21
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 8th Apr 2008 04:11
Jason
I usually do something like



What are the advantages to your approach?

Thanks in Advance

Codger

System
PIV 2.8 MZ 512 Mem
FX 5600 256 mem
jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 8th Apr 2008 04:28
One might say its in a class... and thats enough... but I won't because that's not good enough.

Your approach is fine, except you do call the timer function twice in your example.

In my method of coding, I try not to do stuff twice - like call a function that calls the operating system that calls a bios function that returns values in the system clock area (It might be less than this.. but I think you get the idea)

I actually have a clock class I use that I instantiate once in my program, and call its update method once per loop. I then "Ask this clock" what the timer "was" when I called it - more or less to recycle the value I got from the timer. I also convert the timer to a float and save that - so any math I may do later in my program (as many time based calculations might accur and without thinking ahead - there might be a lot of CPU cycles lost due to converting integer to floats during math calcs - say for timer based animation, movement or whatever.

so the Timer class I wrote takes the "dbtimer()" value as a parameter - and thats what I used in my above example - I tried to show this technique by having the int mytimer thing declared outside the loop - and calling timer at the top - (that you asked about earlier) so that I only caller Timer once per loop.

here is my clock class:

jgc_clock.h


jgc_clock.cpp


Example:


It's not so much about the one timer - it more about planning for having 50 timers, and tons of guns firing, and trying not to call slow functions more than necessary. One or two slow calls doesn't matter - not even noticable - but overall - because projects get large quickly - I try to keep a REAL close eye on efficiency. This doesn't mean I always hit the mark - and frankly your code is perfect in my opinion if you called dbTimer() just the once.

I don't know if this answers your question - is food for thought - or you think this rubbish (I do write rubbish code on occasion I assure you) - but this is why I do it this way.

Codger
21
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 8th Apr 2008 05:38
Thank you very much for such a complete answer. It is very much food for thought.
I understand your point of multiple calls to a system function and the overhead that would take.
I use classes only in a very fundamental way, basically as place holders for grouping data. I am trying to improve my c++ understanding but it is slow going.

Thanks Again

Codger

System
PIV 2.8 MZ 512 Mem
FX 5600 256 mem

Login to post a reply

Server time is: 2024-11-20 07:32:25
Your offset time is: 2024-11-20 07:32:25