Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat
hitTestPoint gives error: attempt to index local 'event' (a nil value) - Gideros Forum

hitTestPoint gives error: attempt to index local 'event' (a nil value)

Hi, I’m having a problem with hitTestPoint. Apparently I don’t know how to use it. The code below (which is the entire code in the project) works fine as long as the two commented lines are commented out. It loads normally and if I click anywhere, the print function prints out as expected.

If I uncomment those two lines, the program runs fine at first, but if I click on the image I get:
Uploading finished.
main.lua:3: attempt to index local 'event' (a nil value)
stack traceback:
main.lua:3: in function

I don’t understand why there are no values for “event”. Can someone help?

local image1 = Bitmap.new(Texture.new("images/randy.png", true))
function image1:printThis(event)
--if image1:hitTestPoint(event.x, event.y) then
print("printing function")
image1:addEventListener(Event.MOUSE_UP, image1.printThis)
stage:addChildAt(image1, 1)


  • hgy29hgy29 Maintainer
    Accepted Answer
    Hi @rpallen, try either of these changes:

    Option 1, turn ':' into '.' on the handler
    function image1.printThis(event)
    Option 2, add image1 as last arg when registering event listener
    image1:addEventListener(Event.MOUSE_UP, image1.printThis,image1)
  • Thanks a lot, hgy29. I tried both options and they both worked. But of course, you knew that. haha

    I've always had problems knowing the difference bewteen the dot operator and the colon operator - instance vs method stuff. I guess I need to read about those again in the lua documentation.

    Thanks again.
  • hgy29hgy29 Maintainer
    function a:b(c)
    is a shorthand for
    function a.b(self,c)
    is a shorthand for
    +1 -1 (+3 / -0 ) Share on Facebook
Sign In or Register to comment.