modular class and EventListeners

Thx to guys on http://blog.anscamobile.com/2011/09/tutorial-modular-classes-in-corona/comment-page-1, I can make a no module class.
But I face the following pb, explained in this simplified version of dog.lua class :

my main.lua:

local dog = require( "class\_dog" )  
  
local dog1 = dog.new( "Yai-Ya",100, 100, 1 )  
  
local function dogTouch(event)  
 if event.phase == "ended" then  
 print("touched", event.target.id)  
 end  
end  
  
dog1:tellmyID()  
dog1:addEventListener("touch", dogTouch)  
-- this returns an error :  
-- method 'addEventListener' a nil value  
-- OK, I have not declared it in class\_dog  
  
-- BUT, I want to add listeners here  
-- my dogTouch function has to stay in main.lua  
-- so ...?????????????????  

my class :

-------------------------------------------------  
  
-- dog.lua  
  
-------------------------------------------------  
  
local dog = {}  
local dog\_mt = { \_\_index = dog } -- metatable  
  
-------------------------------------------------  
-- PRIVATE FUNCTIONS  
-------------------------------------------------  
  
-------------------------------------------------  
-- PUBLIC FUNCTIONS  
-------------------------------------------------  
  
function dog.new( name, posx, posy, dogid ) -- constructor  
  
 local newDog = {  
 name = name or "Unnamed",  
  
 id = dogid  
 }  
  
 newDog.imageMain = display.newImageRect("dog.png",20,20)  
 newDog.imageMain.x = posx;  
 newDog.imageMain.y = posy;  
  
 --newDog.:addEventListener("touch", dogTouch)  
 -- can't add listeners like that ?...  
  
 return setmetatable( newDog, dog\_mt )  
end  
  
-------------------------------------------------  
  
function dog:tellmyID()  
  
 print( self.name .. " ID is " .. self.id )  
end  
  
-------------------------------------------------  
  
return dog  

I know I miss something … any help would be welcomed :)) [import]uid: 9328 topic_id: 17091 reply_id: 317091[/import]

yes, dog is not a display object or one to which you can attach an event listener.

In your code on line 26, the display object is imageMain,

so you can use

dog1.imageMain:addEventListener(“touch”,dogTouch)

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 17091 reply_id: 64230[/import]

Ooohhh …
Thx Jayant ! [import]uid: 9328 topic_id: 17091 reply_id: 64234[/import]

… but there’s one more thing :
What if I want to get the dog.name when clicking on the image :
I tried in main.lua :

[code]
local function dogTouch(event)
if event.phase == “ended” then
print(“touched”, event.target.id)
– works only if I add in dog class :
– newDog.hitzone.id
– but it’s poor since I already have dog.id …
print(“touched”, event.target.parent.name)–return nil
– how to get the name ???
end
end

dog1:tellmyID()
dog1.hitzone:addEventListener(“touch”, dogTouch)
[/code] [import]uid: 9328 topic_id: 17091 reply_id: 64237[/import]

You cannot access the dogs parent that way :wink:

if you want to access the dog object, the best way is to have it stored in the image object, as everything in Lua is a pointer… so as to say, read this article

so in line 29 (above of your main code), just add

newDog.imageMain.Dog = newDog

now in the code you just posted, on line 7 change to

print(“touched”, event.target.Dog.name)

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 17091 reply_id: 64239[/import]

Okayy.
I think I understand :slight_smile:
I’ll try to work a little bit and may be come back with more tricky questions…
Thx for your patience. [import]uid: 9328 topic_id: 17091 reply_id: 64240[/import]