Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Event Listener attributes — Gideros Forum

Event Listener attributes

adityaaditya Member
edited January 2014 in General questions
Have a lua file and a declared Core.class in it.
pScreen = Core.class(Sprite)
Now when I do this, I get "attempt to index field '?' (a nil value)"
1) function pScreen:backButtonClicked(data,event)
2) self.backButton:addEventListener(Event.MOUSE_UP, self.backButtonClicked,{self.backButton})
But this works
1) function backButtonClicked(data,event)
2) self.backButton:addEventListener(Event.MOUSE_UP, backButtonClicked,{self.backButton})
Why?
Loon Games LinkedIn Facebook Twitter - "Something bit me, gaah!"

Comments

  • ar2rsawseenar2rsawseen Maintainer
    edited January 2014 Accepted Answer
    The problem is, that:
    function pScreen:backButtonClicked(data,event)
    is actually a
    function pScreen.backButtonClicked(self, data,event)
    and when you add it as
    self.backButton:addEventListener(Event.MOUSE_UP, self.backButtonClicked,{self.backButton})
    what it receives is:
    function pScreen.backButtonClicked({self.backButton},event)
    while it expects:
    function pScreen.backButtonClicked(self, data, event)
    So to make it work, you should do:
    --1) 
    function pScreen:backButtonClicked(event)
        --self.backButton was clicked
    end
     
    --2) some where in other method
    self.backButton:addEventListener(Event.MOUSE_UP, self.backButtonClicked,self)
    Hope that helps ;)
  • Got it. Thanks!
    Loon Games LinkedIn Facebook Twitter - "Something bit me, gaah!"
  • Am still a tad confused. Sorry again.

    Using your solution and event:getTarget() works. But how do you pass external data in the addEventListener above?
    Loon Games LinkedIn Facebook Twitter - "Something bit me, gaah!"
  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    @aditya you don't, you simple make it as a property of a class, like this:
    function MyClass:init()
        self.custom = "myCustom data"
        self:addEventListener(Event.MOUSE_UP, self.onMouseUp, self)
    end
     
    function MyClass:onMouseUp(e)
        if self:hitTestPoint(e.x, e.y) then
            print(self.custom) -- prints myCustom data
        end
    end
  • Question, if a function is declared as pScreen.backButtonClicked instead of pScreen:backButtonClicked, will the scenemanager still be able to dereference it if the scene is changed?
    Loon Games LinkedIn Facebook Twitter - "Something bit me, gaah!"
  • ar2rsawseenar2rsawseen Maintainer
    edited January 2014
    You can think of it as a static function and yes it could be referenced even if the scene was changed, cause it would actually work without any instance.
    And in this case, what ever you pass to the adEventListener as a third argument will be received in this function as first, for example:
    function MyClass:init()
        self.custom = "myCustom data"
        self:addEventListener(Event.MOUSE_UP, self.onMouseUp, self)
    end
     
    function MyClass.onMouseUp(self, e)
        if self:hitTestPoint(e.x, e.y) then
            print(self.custom) -- prints myCustom data
            --but only because we passed instance as argument, not because it is a part of the class
        end
    end
    So you could actually make it work with any instance, even from completely other class, by passing reference to instance like this
  • Okay.
    Loon Games LinkedIn Facebook Twitter - "Something bit me, gaah!"
Sign In or Register to comment.