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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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 ...????????????????? |
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,
?:)
Ooohhh ...
Thx Jayant !
... 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 :
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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) |
You cannot access the dogs parent that way ;)
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,
?:)
Okayy.
I think I understand :)
I'll try to work a little bit and may be come back with more tricky questions...
Thx for your patience.