I have learned how to make classes with metatables using this very nice tutorial: http://www.coronalabs.com/blog/2011/09/29/tutorial-modular-classes-in-corona/
My problem is that instead of creating a DisplayObject within the class using the constructor, I already have a DisplayObject and I’d like to turn it into a class. Whenever I try doing it, I lose the original metadata stored in the DisplayObject and it doesnt work well anymore.
hero.lua
[lua]local hero = {}
local hero_mt = { __index = hero }
function hero.new(heroDisplayObject)
local newHero = heroDisplayObject
newHero.name2 = “Mustache Guy”
return setmetatable( newHero, hero_mt )
end
return hero[/lua]
main.lua
[lua]local heroDisplayObject = display.newRect(200, 200, 30, 30)
heroDisplayObject.name = “Mario”
print (heroDisplayObject.name, heroDisplayObject.x)
hero = require(“hero”).new(heroDisplayObject)
print (hero.name, hero.x, hero.name2)[/lua]
output:
Mario 215
Mario nil Mustache Guy
As you see, I have lost .x from the original DisplayObject, which is a metamethod I believe. How can I keep the original meta data when creating the class?