DisplayObjects and Inheritance

Hey,

I just thought I’d share this little tidbit. I don’t know whether you use an Object Orientated approach to your Corona Development, but if you do, neatly fitting display objects into any hierarchy you use can be a pain, particularly if you try to follow the model set out in Programming in Lua.

I figured I’d sit down and figure out an approach which would allow me to create a custom display group in much the same way I’d create any other object in Lua. A bit of headscratching later, and this is want I came up with

So here’s an example of how I’ve managed to create a custom display group:

CustomClass={}  
  
function CustomClass:new()  
 o=display.newGroup()  
 local metatable=getmetatable(o)  
 local index=metatable.\_\_index  
 metatable.\_\_index= function (table,key)   
 if CustomClass[key] then  
 return CustomClass[key]  
 end  
 return index(table,key)  
 end  
  
 return o  
end  
  
function CustomClass:newFunction()  
end  

Usually a constructor function which creates a new group and defines new function on it would be a lot simpler to understand (and probably insulate you more from changes to the SDK), but this approach is a lot more consistent, and I think, will make inheritance cleaner to implement.

Let me know what you think! [import]uid: 11757 topic_id: 21470 reply_id: 321470[/import]