Hotspot Types
Blitz3D Forums/Blitz3D Programming/Hotspot Types
| ||
I have recently learnt how to use Types to good effect but have ran into a little problem. I have created a type to hold Hotspots on a screen i.e when the mouse moves to a certain area on screen and colides with the hotspot, the pointer changes to a hand icon. When moving away from the hotsopt it should turn back into a pointer again. In this case it does not. Its slightly doing my nut but it is probably something i missed. Code as follows with 2 on screen hotspots, (2 images are required for the collision) Type HOTSPOT Field HID% ; Hotspot ID Field HX% ; X Pos Field HY% ; Y Pos Field HW% ; Hotspot Width Field HH% ; Hotspot Height End Type Graphics3D 300,300,32,2 SetBuffer BackBuffer() Global hand=LoadImage("hand.bmp"); Mouse Pointer Global pointer=LoadImage("redsq.bmp"); Mouse Pointer HandleImage hand,7,0 HotCreate(0,0,0,10,10) HotCreate(1,100,0,10,10) HidePointer() While Not KeyHit(1) Cls hotspotupdate() Flip Wend End Function HotCreate(HID%,HX%,HY%,HW%,HH%) H.HOTSPOT = New HOTSPOT H\HID% = HID% H\HX% = HX% H\HY% = HY% H\HW% = HW% H\HH% = HH% Return True End Function Function hotspotupdate() For H.HOTSPOT = Each HOTSPOT If H\HID%=0 If RectsOverlap(H\HX%,H\HY%,H\HW%,H\HH%,MouseX(),MouseY(),1,1) DrawImage hand,MouseX(),MouseY() If MouseDown(1) Text 100,100,"Hotspot!!!!" End If End If Else If H\HID%=1 If RectsOverlap(H\HX%,H\HY%,H\HW%,H\HH%,MouseX(),MouseY(),1,1) DrawImage hand,MouseX(),MouseY() If MouseDown(1) Text 100,100,"Hotspot2!!!!" End If End If Else DrawImage pointer,MouseX(),MouseY() End If Next Return True end Function |
| ||
The If..ElseIf..Else conditional in your For..Each loop will never enter your Else block; the loop will iterate twice, once for each of your hotspot objects. Whenever possible, eliminate nested blocks to simplify your code. Your logic can be broken down into two steps: (1) figure out which hotspot the mouse is over, then (2) draw the correct mouse cursor. It's also better to poll your mouse position only once, since it can change while your code is executing, leading to confusing results! Try this instead: local mouse_x = mousex() local mouse_y = mousey() local active_hotspot.hotspot = NULL for h.hotspot = each hotspot if RectsOverlap(H\HX%,H\HY%,H\HW%,H\HH%,Mouse_X,Mouse_Y,1,1) then active_hotspot = h : exit next if active_hotspot <> NULL then DrawImage hand,Mouse_X,Mouse_Y if mousedown(1) then text 100,100,"clicked hotspot id " + active_hotspot\HID + "!" else DrawImage pointer,Mouse_X,Mouse_Y endif |
| ||
Octothorpe, Thanks v much for this refined bit of code. It works a treat....:-) I thought a NULL check would need to be used somewhere to see if the mouse was over a Hotspot or not. This nicely tidies up the hotspot checking process. Types though tricky to get you ed around are indeed fab and v powerfull... Thanks again... |