Question about Modular Classes tutorial

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]

I asked a question that was in kind of the same during the first module tutorial.

When I put listeners outside the return dog things didn’t work, same with functions.
But when I put them inside things worked, which I’m still trying to figure out why. You are having slightly different issues so I have no idea.

ng
[import]uid: 61600 topic_id: 15894 reply_id: 58805[/import]

so, i figured hot to assign touch listeners to every instance, but now i have different situation

how can i make this kind of things work? i can remove one display object, but when i try to remove another i get an error that object already removed

main.lua
[lua]local dog = require(“mod”)
local dog1 = dog.new(“coconut.png”)

local bog2 = dog.new(“kiwi.png”)

bog2:destroy()
dog1:destroy()[/lua]

mod.lua
[lua]local dog = {}
local dog_mt = {__index = dog }
—??? ???----------
local function getDogYears(realYears)
return realYears * 7
end
-----??? ???-----------

function dog.new(name)

local newDog = {
name = name or nil
}

local imageName = display.newImage(name)
function dog:destroy()
imageName:removeSelf()
end
return setmetatable(newDog, dog_mt)
end

return dog[/lua]

i really trying to understand module behavior, so any help will be appreciated [import]uid: 16142 topic_id: 15894 reply_id: 58893[/import]

I think I saw something (I haven’t tried it, I just saw it so yea)

I think when you remove display objects, you also have to nil them
Here is a reference I found:

ie.

tom = objectType.new()  
  
--  
  
tom:destroy()  
tom = nil  

So using your example wouldn’t it be:

bog2:destroy()  
bog2 = nil  
dog1:destroy()  
dog1 = nil   
  

?? Hmm?

this came from this link, about the modular classes:

(search for “@Thomas – Re destroying kittens”) it was a response to a question about it.

http://blog.anscamobile.com/2011/09/tutorial-modular-classes-in-corona/

Let me know if it works, I’ll try it out later. :slight_smile:

[import]uid: 61600 topic_id: 15894 reply_id: 58915[/import]

thanks for trying, but its not working
i figured that this type of module behavior is not working for me, so i will be using something easier [import]uid: 16142 topic_id: 15894 reply_id: 59169[/import]