Using Keydown for firing without slowdown....?

Blitz3D Forums/Blitz3D Programming/Using Keydown for firing without slowdown....?

elseano(Posted 2004) [#1]
Basically, I want to use the keydown command - not keyhit - for a game, but I don't want a constant stream of bullets:
||
||
||
I want something more like this:
||

||

||
But still without using the keyhit function, because I think it's annoying when you have to keep 'button bashing' the fire button. Has anyone got any ideas?


RifRaf(Posted 2004) [#2]
use a timer..

Beginning of game..

Global Gametimer#
Global FireTimer#


In main loop

Gametimer#=Millisecs()

Then before you fire a bullet with keydown

If keydown(whatever) then
If firetimer#<gametimer# then
Firetimer#=gametimer#+200 ; thats 200 ms between shots
Firebullet_here()
endif
endif


elseano(Posted 2004) [#3]
Thanks a lot! :D


semar(Posted 2004) [#4]
Altough RifRaf has given a very good advice here, I would add another method, which uses two variables, one for fire pause, and the other as a flag which should be decreased in the main loop:
Global fire_pause = 50
Global enable_fire = fire_pause

;the game main loop
while not keydown(1) ;quits with ESC

;always decrease the fire pause
if enable_fire > 0 then
enable_fire = enable_fire - 1
endif

if keydown(fire_button) ;pressed fire ?

if enable_fire = 0 then ;enabled to fire ?

fire_your_bullet() ;fire !

enable_fire = fire_pause ;reset the fire flag

endif
;.
;.
wend ;end of the main loop


During the game, if you want to modify the pause between the shoots - for example when the player gets some bonus -
you may decrease the value of the fire_pause variable, and then the player shoots will be faster.

Hope this has sense for you,
Sergio.


elseano(Posted 2004) [#5]
Thanks, this is a great way of doing it too.


Genexi2(Posted 2004) [#6]
Just make sure your project has frame-skippin' in it, or else RifRaf's method (which I use as well) can cause some cheatin' moments on slower PC's....