Pants!
I’d been using the manual linking myself and hoped to move onto a cleaner system, but was struggling to get it working.
There is actually a clean way that works a bit better than manually linking, you can use pairs(baseclass) to map over all the fields you want to your custom display group.
So my hope had been to have a prototype based system where man.lua, handled all manly things. Civilian.lua then supplemented Man with civilised things and the display group did the keeping all the sprites in one place thing.
Here’s what I’ve got:
Civilian=Man:clone()
function Civilian:new()
local inst = display.newGroup()
-- map all fields in Civilian and Man onto display group
for k,v in pairs(Civilian) do
inst[k]=v
end
return inst
end
I’ll probably move the creating of a new display group and mapping the fields over into it’s own function, in case I want to subclass civilian later.
The clone method I’m using is as follows:
function clone( base\_object, clone\_object )
if type( base\_object ) ~= "table" then
return clone\_object or base\_object
end
clone\_object = clone\_object or {}
clone\_object.\_\_index = base\_object
return setmetatable(clone\_object, clone\_object)
end
From http://lua-users.org/wiki/InheritanceTutorial
I had been hoping to be able to pass display a Group instance in as the clone_object param, but none of the functions were preserved for some reason. Partly because I think the Corona display objects are implemented in a slightly weird way. Try using pairs to print of a list of the group’s functions and fields. It doesn’t produce the results I expected!
[import]uid: 11757 topic_id: 11059 reply_id: 41454[/import]