Using getSpriteHit would only work if you have a sprite as your pointer. Also, for selecting GUI components it's inefficient.
It's christmas eve and I'm at work, so..... I'll make a demo for ya cause I'm bored!
It's very little code, maybe 50 lines if you take out the spacing and all the comments. This uses a UDT array to keep track of multiple checkboxes. I made an image for an animated checkbox. And not just animated in terms of having a checkbox on or off state, but a glowing effect when it's active. (like I said, I got bored).
Simple functions let you create a checkbox, position it, get it's current state, and a single function call in the main loop handles everything.
setVirtualResolution(640,480)
// How many checkboxes the array can hold
#CONSTANT CHECKBOX_MAX = 10
// This equals the number of active checkboxes
GLOBAL _CheckBox_Index = 0
// Variables for each checkbox
Type T_CheckBox
sprite as integer
state as integer
btnDown as integer
EndType
// array for checkboxes (since agk doesn't have dynamic arrays)
dim checkboxes[CHECKBOX_MAX] as T_CheckBox
// create a checkbox
n1 = createCheckbox()
positionCheckbox(n1, 200, 100)
n2 = createCheckbox()
positionCheckbox(n2, 300, 200)
do
checkboxHandler()
print("Checkbox 1: "+str(getCheckboxState(n1)))
print("Checkbox 2: "+str(getCheckboxState(n2)))
Sync()
loop
////////////////////////////////////////////////////////////
// Handles checkbox functionality
////////////////////////////////////////////////////////////
function checkboxHandler()
// Get mouse coordinates
mx = getRawMouseX()
my = getRawMouseY()
btnLeftPressed = getRawMouseLeftPressed()
btnLeftReleased = getRawMouseLeftReleased()
btnLeftState = getRawMouseLeftState()
// Loop through all checkboxes
for i = 1 to _CheckBox_Index
// Make sure mouse is over the checkbox
// Checkbox sprite is offset so it's positioned by it's center. Use simple distance formula (pythagora)
// to see if mouse is within bounds of circular checkbox. In this demo, image is 32px, so 32*32 is 256.
if (mx-getSpriteXByOffset(checkboxes[i].sprite))^2 + (my-getSpriteYByOffset(checkboxes[i].sprite))^2 <= 256
// If checkbox has not been clicked yet
if checkboxes[i].btnDown = 0
// if left mouse button pressed
if btnLeftPressed = 1 then checkboxes[i].btnDown = 1
endif
// If checkbox has already been clicked on but mouse button has not released yet
if checkboxes[i].btnDown = 1
// If mouse button released then toggle the checkbox state
if btnLeftReleased = 1
if checkboxes[i].state = 0
checkboxes[i].state = 1
playSprite(checkboxes[i].sprite, 12, 1, 2, 10)
else
checkboxes[i].state = 0
stopSprite(checkboxes[i].sprite)
setSpriteFrame(checkboxes[i].sprite, 1)
endif
endif
endif
endif
if btnLeftState = 0 then checkboxes[i].btnDown = 0
next i
endfunction
////////////////////////////////////////////////////////////
// Create new checkboxes whenever you want easily
////////////////////////////////////////////////////////////
function createCheckbox()
if _CheckBox_Index = CHECKBOX_MAX then exitfunction 0
inc _CheckBox_Index
c = -1
if _CheckBox_Index = 1
c = createSprite(loadImage("checkbox.png"))
else
c = cloneSprite(checkboxes[1].sprite)
endif
setSpriteAnimation(c, 32, 32, 12)
setSpriteOffset(c, 16, 16)
checkboxes[_CheckBox_Index].sprite = c
endfunction _CheckBox_Index
////////////////////////////////////////////////////////////
// Position your checkbox
////////////////////////////////////////////////////////////
function positionCheckbox(id, x, y)
setSpritePositionByOffset(checkboxes[id].sprite, x, y)
endfunction
////////////////////////////////////////////////////////////
// Returns the state of a checkbox
////////////////////////////////////////////////////////////
function getCheckboxState(id)
s = checkboxes[id].state
endfunction s
Another option if you really want to delve into GUI design, you can take a look at my iOS library to see how I did a lot of different things.
https://forum.thegamecreators.com/thread/210459
"I like offending people, because I think people who get offended should be offended." - Linus Torvalds