Hi Vonncc123,
Can I try and answer besides the question?
The meaning of the code, as you surely know, is to set metatables of one objects to the metatable of another.
Personally, I always found this code to be very confusing. I could never remember it and so always had to copy it from a template file. Even today I can’t remember exactly what the syntax or order is.
For the vast majority of use cases (i.e. just writing modules for your game characters and enemies etc…) I would suggest that you leave metatables alone and go for the vastly more understandable modular pseudo-object oriented programming.
That means, for an enemy you create something like this:
-------------------- -- enemyClass.lua -- -------------------- local enemyClass = {} enemyClass.new = function(parent) -- parent is optional but handy, read below local self = {} self.type = "enemy" self.parent = parent -- passing the parent to child objects make it supereasy for them to talk to their "parents" -- after this you can add all the displayObjects you want self.mainGroup = display.newGroup() self.rectangle = display.newRect(self.mainGroup, 200,200,100,100) -- and then you can write all the functions you want self.changeColor = function() local randomRed = math.random(255)/255 local randomGreen = math.random(255)/255 local randomBlue = math.random(255)/255 self.rectangle:setFillColor(randomRed, randomGreen, randomBlue) end -- self.changeColor() self.frameLoop = function(event) self.rectangle.rotation = self.rectangle.rotation+1 end -- self.frameLoop() -- and in the end you can add all timers or evenListener you want to Runtime:addEventListener("enterFrame", self.frameLoop) self.changeColorTimer = timer.performWithDelay(1000, self.changeColor, 0) -- and then lastly it's best to write a "kill" function that you can call from the parent, to clean up the object self.kill = function() -- 1 first clean up the timers, transition and eventListeners Runtime:removeEventListener("enterFrame", self.frameLoop) timer.cancel(self.changeColorTimer) -- 2 then clean up all child objects, if there are eny -- in this case there are none -- 3 then clean up all the displayObjects -- 4 if needed, set any non-local variables to nil -- typically if you code well, you won't have any global variables so this step if unnecessary end -- self.kill() return self end -- enemyClass.new() return enemyClass
Then what you do, is in a parent module, you require the “enemyClass.lua”, and then create a new object like this:
parentObject.myEnemy = enemyClass.new(parentObject) – where self is the parent object
In reality, if you code everything like this, the parentObject will also refer to itself by the term “self”, so it would probably look like this:
self.myEnemy = enemyClass.new(self)
Or even more realistic, you will have multiple enemies in a table, so creating will look like this, called from the parent:
self.enemyList[1] = enemyClass.new(self)
I hope this make some sense to you, although I can image if this is new stuff, it’s confusing at first. But I can tell you that for me personally this is the best, fastest, clearest and easiest way to do things.
If you can’t manage to make this work, let me know and I’ll write you some more demo code.