Hi, Endy!
First of all I would recommend you to avoid using reserved words like “self” as your local variables names as it may cause hardly discoverable mistakes in future.
Considering your question it looks like Corona’s display objects have complicated logic inside of themselves. And they already have metatables assigned to them.
By doing
[lua]setmetatable(self, Platform )[/lua]
where " self " is a display group you actually overwrite its metatable. Thus you get an unexpectable behaviour.
It is easy to check. Try this code
[lua]local myTab = { a = ‘1’ }
local myTab2 = { a = ‘2’ }
print(myTab.a)
setmetatable(myTab, myTab2)
print(myTab.a)[/lua]
You will see expected result:
1
1
But if you try this code
[lua]local myDG = display.newGroup()
myDG.myCustomProperty = ‘myVal’
print(myDG.numChildren, myDG.myCustomProperty)
local myTab3 = {myCustomProperty = ‘someNewVal’}
setmetatable(myDG, myTab3);
print(myDG.numChildren, myDG.myCustomProperty)[/lua]
you will get
0 myVal
nil myVal
From this we may conclude that “numChildren” property is actually inherited by display group from its initial metatable.
If you try to add this row to your code
Platform.numChildren = 99
you will see ‘99’ as the result of print(“num Children:”, self.numChildren).
Now how to make things work right. Just create a new table object with your display group as a property of that table. And then set metatable to your table like this:
[lua]function Platform.new(x,y, width, height, type )
local newPlatform = {}
newPlatform.group = display.newGroup()
local test = display.newCircle(30,10,10,10,10)
newPlatform.group:insert(test)
print(“num Children:”, newPlatform.group.numChildren)
setmetatable( newPlatform, Platform )
print(“num Children:”, newPlatform.group.numChildren)
return newPlatform;
end[/lua]
And at least a few words about your class template. I wonder how you include this class into your main.lua. I usually look at my classes as on more or less independent modules so I can write require(‘myClass’) when needed.
How do you include this template into your main.lua?
Here is my usual class template:
[lua]local Platform = {}
function Platform:new(x,y, width, height, type )
local newPlatform = {};
function newPlatform:init()
newPlatform.group = display.newGroup()
newPlatform.group:insert(display.newCircle(30,10,10,10,10))
print(“num Children:”, newPlatform.group.numChildren)
end
function newPlatform:someOtherFunction( … )
– body
end
newPlatform:init();
self.__index = self
return setmetatable(newPlatform, self)
end
return Platform[/lua]
If I’ve saved this file as platform.lua then I can easily call it from where I need by using
[lua]local _Platform = require(“platform”)
local myPlatform = _Platform:new(x,y,w,t)[/lua]
I hope this was helpful.
Best regards,
Anton