Posted: 16th Aug 2011 9:25
I've created a sprite called spr_menu_1_normal, is it possible to do the following?

+ Code Snippet
if getpointerpressed() = 1
  if getspritehit (getpointerx(), getpointery()) = spr_menu_1_normal
    ...
    ...
  endif
endif


I've tried and it doesn't seem to register the click on that sprite. If I comment out the "if getspritehit..." line then the (global) click registers.
Posted: 16th Aug 2011 9:37
Might be down to local / global scope. Is the variable spr_menu_1_normal a global?
Posted: 16th Aug 2011 9:40
Might be down to local / global scope. Is the variable spr_menu_1_normal a global?


Err... not sure, pretty new to this stuff. Here's the full code:

+ Code Snippet
rem
rem AGK Application
rem

rem Portrait App
setvirtualresolution(480,800)

rem: Load menu images
img_menu_1_normal = loadimage ("begin.png")
img_menu_1_over = loadimage ("begin_over.png")


rem: Set images as sprites
spr_menu_1_normal = createsprite (img_menu_1_normal)
spr_menu_1_over = createsprite (img_menu_1_over)

rem: Hide the 'clicked' menu item
setspritevisible (spr_menu_1_over,0)




do

    if getpointerpressed() = 1
        if getspritehit (getpointerx(), getpointery()) = spr_menu_1_normal
          setspritevisible (spr_menu_1_normal,0)
          setspritevisible (img_menu_1_over,1)
        endif
    endif
 Sync()
loop
Posted: 16th Aug 2011 10:43
Ok, this is odd, I changed

img_menu_1_normal = loadimage ("begin.png")

to

loadimage (1,"begin.png")

and

spr_menu_1_normal = createsprite (img_menu_1_normal)

to

createsprite (1,1)

and tried

if getspritehit (getpointerx(), getpointery()) = 1

but still no joy. After a bit of debugging it seems it's looking for sprite 10001! If I put

if getspritehit (getpointerx(), getpointery()) = 10001

it works! Am I doing something wrong creating the images / sprites?
Posted: 16th Aug 2011 10:45
The problem here is that GetSpriteHit is returning the ID of spr_menu_1_over as this sprite is being drawn above spr_menu_1_normal. It should really detect that this sprite is invisible and ignore it when GetSpriteHit is being used.
Posted: 16th Aug 2011 10:51
Ahh! So even though sprites are invisible they are still being detected? So moving the over sprite would solve it? Thanks Mike, would never have thought of that.

Edit: Yup, that did the trick.