I’m currently implementing classes like in my code snippet below (although it may be better to use something that has been developed more fully eg http://love2d.org/wiki/MiddleClass)
however i’m a bit confused between modules and these “classes”. I looked at the movieclip.lua example and it seems like the whole “class” is wrapped in a function
what I’m not sure about in mine is how to create a public static variable. I have a private static variable which works fine.
Can I just do:
ParallaxLayer:MY_STATIC_VARIABLE = “something”
?
are there benefits of using modules over what I am trying to do here?
main.lua
[lua]require(“ParallaxLayer”)
local p = ParallaxLayer:new(“whatever”)
print(p:getPrivateID())[/lua]
ParallaxLayer.lua
[lua]ParallaxLayer= {} – Create a table to hold the class methods
ParallaxLayer.__index = ParallaxLayer
local STATIC_ID – private, static
local _private = setmetatable({}, {__mode = “k”}) – private var container
function ParallaxLayer:new(layer)
print("ParallaxLayer::new " … layer)
local object = {
id = layer; – public variable
}
_private[object] =
{
_id = layer … " (private)"
}
setmetatable(object, ParallaxLayer) – Inheritance
if(STATIC_ID == nil) then
STATIC_ID=1
else
STATIC_ID = STATIC_ID + 1
end if
return(object)
end
function ParallaxLayer:getID()
return self.id
end
function ParallaxLayer:getPrivateID()
print(“ParallaxLayer::getID”)
print(“static STATIC_ID=” … STATIC_ID)
print("private _id = " … _private[self]._id
print(“public id =”… self.id)
return(_private[self]._id)
end[/lua] [import]uid: 6645 topic_id: 3512 reply_id: 303512[/import]