Mouse Clicking in areas

Blitz3D Forums/Blitz3D Beginners Area/Mouse Clicking in areas

Uraliss(Posted 2003) [#1]
Hi Folks,
Whats the best/easiest wasy of determining if the user has clicked on an area of the screen in a 2d game?

Any ideas?


Blue Steel(Posted 2003) [#2]
look in the code archives something in there for "hotzones". very useful in BP


GfK(Posted 2003) [#3]
Use my ZoneLib.
; ZoneLib by Dave Kirk, 30th March 2001
; This is a set of functions for creating ScreenZones, like in AMOS.

; ZLReserveZone		- Set the maximum number of zones to use (reserves some memory).
; ZLSetZone			- Define a zone.  Must be between 1 and the number specified in ZLReserveZone
; ZLKillZone		- Delete a zone.
; ZLFreeZones		- Delete all zones and free up reserved memory.
; MouseZone()		- Returns the number of the zone the mouse pointer is in.

AppTitle "ZoneLib"

;Globals for ZoneLib - PUT THESE IN YOUR PROGRAM TOO!
Global ZoneLib%,NumZones

;Demo code
Graphics 800,600

ZLReserveZone(5)

SetBuffer BackBuffer()

ZLSetZone 1,50,50,150,70
ZLSetZone 2,200,300,240,380
While KeyDown(1)=0
	If MouseDown(2) Then ZLKillZone 2
	Cls
	Color 255,255,0
	Rect 50,50,100,20,True
	Rect 200,300,40,80,True
	Z = MouseZone()
	Locate 0,0 : Print "Zone:" + Z
	Oval MouseX()-2,MouseY()-2,4,4,True
	Flip
Wend
EndGraphics
End
;End of demo code




;ZONELIB FUNCTIONS
Function ZLReserveZone(Num%)
	ZoneLib = CreateBank(Num*8)
	NumZones = Num
End Function

Function ZLSetZone(Zone,X1,Y1,X2,Y2)
	M = (Zone-1)*8
	PokeShort ZoneLib,M,X1
	PokeShort ZoneLib,M+2,Y1
	PokeShort ZoneLib,M+4,X2
	PokeShort ZoneLib,M+6,Y2	
End Function

Function ZLKillZone(Zone)
	M = (Zone-1)*8
	PokeShort ZoneLib,M,0
	PokeShort ZoneLib,M+2,0
	PokeShort ZoneLib,M+4,0
	PokeShort ZoneLib,M+6,0	
End Function

Function ZLFreeZones()
	FreeBank ZoneLib
End Function

Function MouseZone()
	ZoneNum = 0
	For N = 1 To NumZones
		M = (N-1)*8
		X1 = PeekShort(ZoneLib,M)
		Y1 = PeekShort(ZoneLib,M+2)
		X2 = PeekShort(ZoneLib,M+4)
		Y2 = PeekShort(ZoneLib,M+6)
		If MouseX()>X1
			If MouseX()<X2
				If MouseY()>Y1
					If MouseY()<Y2
						ZoneNum = N
					EndIf
				EndIf
			EndIf
		EndIf
	Next
	Return ZoneNum
End Function
I think someone made an enhanced version based on this code. I can't remember who it was now though...


Uraliss(Posted 2003) [#4]
Thanks again guys!


Psycoach(Posted 2003) [#5]
Excellent !

I was just about trying to write some code for that ! :-)

Thanks for sharing !