Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Why is event listener not working in scene manager? — Gideros Forum

Why is event listener not working in scene manager?

ZizanymanZizanyman Member
edited March 2015 in General questions
I am using scene manager, and whenever I try to use a function in my main menu scene, it brings up an error when I release my mouse or finger.
Code:

11. local function button1touch(button, event)
12. if button:hitTestPoint (event.x, event.y)
13. print"change scene now"
14. end
15.end
stage:addEventListener(Event.MOUSE_UP, button1touch, button)

Error:
Scenes/main_menu.lua:12: attempt to index local 'event' (a nil value)
stack traceback:
Scenes/main_menu.lua:12: in function

Does anybody know why this is happening? Thanks!

Comments

  • hgy29hgy29 Maintainer
    Hi @Zizanyman, not sure why but in your code there seem to have a space between hitTestPoint and the first parenthesis, try to remove it. Also I usually had a 'then' after the condition in the 'if', not sure how lua does without
  • @hgy29, I did both those things and it still brings up the error.
  • I'm almost positive that it is because of scene manager, because the same function in an app that I made that didn't use scene manager worked.
  • piepie Member
    edited March 2015
    @Zizanyman I should see the other code, but I think you're passing a nil parameter with button in
    stage:addEventListener(Event.MOUSE_UP, button1touch, button) --this button
    see attached project in scenes/start.lua, your function works. :)

    However, if I may suggest: don't use Stage as an "event holder" unless you're sure of what you're doing. I did, and I went crazy hunting a memory leak since I wasn't removing some event listeners correctly.

    My "workaround" (maybe not elegant, but seems to work until now :) ) is to create a global event Holder (that is visible everywhere since it's global) and nil it when I need to remove every event listener, so that every eventListener bound to it goes to garbage collection as I ask. :)

    However, I think that one should try to keep as much things as possible local.
    There are good chances that in this case you can add the event listener directly to the button.
    zip
    zip
    changescene.zip
    17K
Sign In or Register to comment.