events and objects.

Hello,

I’ve this very annoying problem and somehow I can’t come up with an solution …so please help.

I have a class which will be instantiated in several modules. Every object of this class has an event listener for a touch event. Only one object can be “alive” at a time so only one event listener exists.

However I can’t figure out how to access “self” inside the callback of the listener (it allows access to the globals of the module but not to the globals of the object, which have different values)

[lua]

–globals

stuff = “init”

function someobj:new()

o = {}
setmetatable(o, self)
self.__index = self
return o
end

function someobj:initStuff()

– setup the touch listener –
Runtime:addEventListener( “touch”, someobj )

self.stuff = “woohoo!”
end

function scrollView:touch(event)
print(self.stuff) – this will print “init” and not “woohoo!”[/lua]

so can I specifically register an listener on a specific object? [import]uid: 11772 topic_id: 4631 reply_id: 304631[/import]

When you pass in “someobj” as the listener then it’ll use the touch method of someobj. That is, someobj:touch(event)

If you want to use the touch method of the scrollView object, then pass in scrollView as the listnener:

Runtime:addEventListener("touch", scrollView)  

Although… did you write scrollView:touch(event) by accident? Obviously “self” will be different in the scrollView object and the someobj object. [import]uid: 12108 topic_id: 4631 reply_id: 14666[/import]

oh yeah that was accidental, sorry for that, it should be someobj but i just found the solution (which is actually pretty obvious)

just adding “self” to the listener does the trick. Then i can access all the self.variables just like in the other methods :slight_smile:

[lua]Runtime:addEventListener(“touch”, self)[/lua]

doing stuff in corona is really easy but the documentation is so sloppy [import]uid: 11772 topic_id: 4631 reply_id: 14668[/import]