MouseHit ( button )
Parameters
|
button - which mouse button to test 1: left button 2: right button 3: middle button |
Description
|
Returns how many times a mouse button has been clicked since you last asked. This is the click command. Buttons on a menu, picking up an item, firing a single shot, placing a tile in an editor - anything that should happen once per press belongs here rather than in MouseDown, which would repeat every frame the button stayed down. The result is a count, so it doubles as a plain yes/no test and also tells you when the player double-clicked inside one frame. Reading it resets the count to zero, which means only one place in your code should test a given button each frame - the first check to run swallows the click and the second sees nothing. If two systems need it, read it once into a variable and share that. A click is registered at the moment the button goes down, and the pointer may well have moved by the time you read it. If exactly where the player clicked matters, sample MouseX and MouseY in the same frame you act on the hit, and keep your per-frame input reading in one place near the top of the loop. FlushMouse clears every pending click at once - handy when a new screen appears so an enthusiastic click from the last one does not immediately press the first button on it. See also: MouseDown, GetMouse, WaitMouse, FlushMouse, MouseX, KeyHit. |
Example
; MouseHit Example ; ---------------- Graphics 640,480,0,2 SetBuffer BackBuffer() ; Hide the Windows pointer - the example draws its own crosshair HidePointer ; One entry per bullet hole on the range Type shot Field x,y End Type count=0 While Not KeyDown(1) Cls mx=MouseX() my=MouseY() ; MouseHit returns how many times the button was CLICKED since ; the last call - holding the button down fires only ONE shot. ; (Compare MouseDown, which stays 1 while the button is held.) hits=MouseHit(1) If hits>0 Then s.shot=New shot s\x=mx s\y=my count=count+hits End If ; Bullet holes left by earlier clicks Color 180,120,60 For s.shot=Each shot Oval s\x-3,s\y-3,6,6,True Next ; Crosshair Color 255,255,0 Oval mx-10,my-10,20,20,False Line mx-14,my,mx+14,my Line mx,my-14,mx,my+14 Color 255,255,255 Text 0,0,"Left click: shoot (one hole per click) Esc: exit" Text 0,20,"MouseHit(1) clicks counted so far: "+count Flip Wend End
Index