Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm having trouble with some bullets in my game, the M1911, which is supposed to be semi-auto is only capable of shooting one projectile per mouse click (trying to learn how to do multiple ones at once). The problem is, if the player clicks the mouse ridiculously fast it causes the bullet to reset its position back to the player (not intended).

Basically I want to limit the clicks, or implement a cooldown after the left mouse buttons pressed which prevents the player from clicking at all during a certain timeframe (or until the bullet hits something).

I want it so that when LMB is pressed it starts a timer.

share|improve this question

closed as too localized by Byte56, msell, Trevor Powell, Nate, Anko May 1 '13 at 22:40

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
If everything runs on one thread keep in mind that your "while" is preventing your program to continue while that statement is true. –  Sidar May 1 '13 at 0:46
1  
Do you really only have one bullet that is teleported back when pressing LMB? This will cause issues when you start guns with a higher fire-rate. You need to have a pool of bullets and some max lifetime per bullet :). –  Roy T. May 1 '13 at 6:24
    
well its just for the M1911 which is semi auto, I'm still learning on how to do that lol :3 If you know a good tutorial somewhere can you link me plz? thx –  Nero-One May 2 '13 at 23:37

1 Answer 1

The usual way to handle this is that every time a bullet is fired you set some variable to your cooldown time. Every frame you decrement that variable by the frame time. If you try to fire and that value is greater than 0, you just ignore the fire input event.

If that feels bad, a next step is to queue up a "they want to fire" action if they try to click when the cooldown is happening and execute on it when the timer passes zero.

share|improve this answer
    
so what I do is I make it so that when they click the variable is set to 1 (for example) and if the variable equals 1 then the left mouse button is disabled, then after the time I want it to cooldown passes, set it back to 0? –  Nero-One May 1 '13 at 0:40
    
OH no wait, Its like a countdown timer huh? the bullet is fired and the variable (lets say 50 or something) is subtracted by the frames being generated, and if the value isn't 0 then the bullet can't be fired? –  Nero-One May 1 '13 at 0:46
1  
Generally you don't want to use frames unless you're frame locked at 60hz and know you're not going to drop below that. It's better to use actual seconds (stored as a float or whatever). But more or less that's accurate. –  Tetrad May 1 '13 at 1:36
    
Take a look at GameTime.ElapsedGameTime.TotalSeconds :) –  Roy T. May 1 '13 at 6:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.