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]
[import]uid: 3826 topic_id: 17091 reply_id: 64230[/import]