Mousedown executes code twice

BlitzMax Forums/BlitzMax Beginners Area/Mousedown executes code twice

Schwang(Posted 2007) [#1]
I can post a quick code example if required.
I've an object that can fire lightning bolts by holding the mouse down. To limit the number of bolts fired per second, I use OnMouseHit(1) and a local timer: every shot sets the timer to zero.
Each game loop increments the timer.
When the timer reaches 10, another shot is allowed.

This works fine.

My problem is simply that using MouseDown(1) fires 2 shots simultaneously. MouseHit() does not do this (but is unsuitable because it requires repetitive clicking).

Because my shots are instant (i.e. they do not travel but hit the target instantly) I use the same function to make an explosion, of which 2 are also created simultaneously.

How can I correct this so only one lightning bolt and one explosion is created?

Note: The only way I could tell two were created is by using a random image for each bolt and each explosion. Two images appear onscreen.


tonyg(Posted 2007) [#2]
You probably will needto post some code but what happens if you assign the result of mousedown(1) to a variable and use this to check if it's OK to do another zap? If not, you reset it to 0.


Schwang(Posted 2007) [#3]
I'll only be able to post the actual code up after work. The heart of the problem looks like this:

...
If ( shot_counter < 10 ) shot_counter:+1
If MouseDown(1) fire_lightning()
...

Method fire_lightning()
If ( shot_counter = 10 )
shot_counter = 0
make_lightning_bolt( x, y )
make_explosion( x, y )
End If
End Method
-------------
MouseDown() just returns a TRUE or FALSE, right?
I've not tried assigning its result to a variable first but I'll give it a shot, though I can't really see how that will solve it.


Brucey(Posted 2007) [#4]
Make shot_counter size bigger.

What happens if you hold down the mouse button? Don't you get a constant stream of bolts and explosions?

If your main loop is iterating 60 times a second, then you will get about 6 shots in a second, by your code...


Schwang(Posted 2007) [#5]
Brucey, that's correct. 6 shots per second.


Czar Flavius(Posted 2007) [#6]
Make a global test variable that goes up by 1 each time a lightening bolt is created. Do a test run and compare the count to the number expected. Then we can be sure if it's a problem with MouseDown or somewhere else in the code.


skidracer(Posted 2007) [#7]
You need to reset your counter if the mouse is not down, i would go for code more like this:
if mousedown
  if shotdelay
    shotdelay=shotdelay-1
  else
    fireshot
    shotdelay=10
  endif
else
  shotdelay=0
endif



TaskMaster(Posted 2007) [#8]
Skid, with that code, a user could fire as fast as they wanted by pressing and releasing the fire button. Whether he wants that, or if he wants to enforce 6 shots per second in that case, I do not know.

I would do a Last Shot variable and just test the time since the last shot. If it has been long enough, fire and set the last shot time to the current time. Rinse, repeat.


Schwang(Posted 2007) [#9]
And the dumbass of year award goes to:
ME!

I had "accidenally" created two instances of the player object, hence two lightning bolts and two explosions.

See, there's a damn good reason this was posted in the n00b forums ;) Works fine now.

Indeed there are many ways to limit the number of shots. I currently use a "cooldown timer" I explained earlier which works fine, formatted properly:

...
If ( shot_counter < 10 ) shot_counter:+1
If MouseDown(1) fire_lightning()
...

Method fire_lightning()
  If ( shot_counter = 10 )
    shot_counter = 0
    make_lightning_bolt( x, y )
    make_explosion( x, y )
  End If
End Method


Thanks for the helpful replies, guys.


TaskMaster(Posted 2007) [#10]
Sure, your method works. But you are incrementing it always. Waste of CPU cycles.