Hello again, sorry for a lot of questions today
I read an blog post http://blog.anscamobile.com/2011/09/tutorial-modular-classes-in-corona/ and found myself in trouble about this particular example
how can i add eventListener to object, that was created using this kind of module?
[lua]local dog = require(“mod”)
local dog1 = dog.new{name = “image.png”,x = 300, y = 200}
local function foo()
print(“dog”)
end
dog1:addEventListener(“touch”, foo)[/lua]
thats wasnt working for my surprise, but then i tried this:
[lua]local dog = {}
local dog_mt = {__index = dog }
local function getDogYears(realYears)
return realYears * 7
end
function dog.new(params)
local newDog = {
name = params.name,
x = params.x,
y = params.y
}
return setmetatable(newDog, dog_mt)
end
local function dog:touch(event)
if event.phase == “ended” then
print(self.name)
end
end
function dog:rollOver()
invName = display.newImage(self.name)
invName.x = self.x or 0
invName.y = self.y or 0
invName:addEventListener(“touch”, touch)
end
return dog[/lua]
and its not good practice either…
so, how can i make created instance respond to touch function and print self.name? i think i do something wrong, because i doubt that i need to set listener inside function
thanks in advance
[import]uid: 16142 topic_id: 15894 reply_id: 315894[/import]
