Instance Event Listeners

What’s best practice for managing event listeners inside classes?

function MyObject:method() local img = display.newImage( 'img.png' ) img:addEventListener( 'touch', self:thisDoesntWork ) img:addEventListener( 'touch, self.thisLosesInstanceScope ) img:addEventListener( 'touch', function() self:cantRemoveThisListenerLater() end ) end [import]uid: 4596 topic_id: 8092 reply_id: 308092[/import]

Ok this works

[code]
function MyObject:method()
self.name = ‘myInstance’
self.touch = _onTouch
local img = display.newImage( ‘img.png’ )
img:addEventListener( ‘touch’, self )
end

function _onTouch( self, event )
print( self.name, event.phase )
end
[/code] [import]uid: 4596 topic_id: 8092 reply_id: 28851[/import]

This sure is a hassle. Any ideas why passing self:method to the addEventListener function doesn’t work?

img:addEventListener( ‘touch’, self:thisWouldBeSoEasy ) [import]uid: 4596 topic_id: 8092 reply_id: 28862[/import]

ah crap I just figured out the function closure approach, but that listener can’t be removed? well that sucks, I guess I should check my code for memory leaks. [import]uid: 12108 topic_id: 8092 reply_id: 28939[/import]

In Lua, in the absence of the “function” keyword, the “:” (colon) is reserved for a function call, not a reference/indexing. Furthermore, Corona makes a significant distinction based on whether it is passed a table, or function to “addEventListener”…it’s internal housekeeping is different. [import]uid: 6175 topic_id: 8092 reply_id: 28972[/import]

ah crap I just figured out the function closure approach, but that listener can’t be removed? well that sucks, I guess I should check my code for memory leaks.

Usually not since you can’t reference the function elsewhere. But there may be some lua way of doing so that I’m not aware of. As best I’m aware adding a .touch reference to an event handler your object, and passing the object to the eventListener is the best way in instances where you can’t just pass a static function to the eventListener. [import]uid: 4596 topic_id: 8092 reply_id: 28978[/import]

In Lua, in the absence of the “function” keyword, the “:” (colon) is reserved for a function call, not a reference/indexing. Furthermore, Corona makes a significant distinction based on whether it is passed a table, or function to “addEventListener”…it’s internal housekeeping is different.

Do you know if there’s another way to pass an instance method to the eventListener. [import]uid: 4596 topic_id: 8092 reply_id: 28980[/import]

Just name the function “touch” on your instance and then pass the table… like this:

local obj = newRect()
obj.name = ‘myObj’

obj.touch = function(self, event) print self.name; end;

obj:addEventListener(‘touch’,obj)

Done!! [import]uid: 6175 topic_id: 8092 reply_id: 28985[/import]

Usually not since you can’t reference the function elsewhere

aha that’s a good point. But that means everything should still be fine for menu buttons that I won’t remove the listener from. whew

Still, I’ll watch out for this next time I’m checking for memory leaks.

ADDITION: Incidentally, another place where I sometimes want to call methods and so I use the function closure approach is in performWithDelay. That would shouldn’t have any problem, right? [import]uid: 12108 topic_id: 8092 reply_id: 29084[/import]