Lua OOP problem

HI.

I have had the problem with OOP in Lua.I have read this article http://www.omidahourai.com/from-zero-to-oo-ardentkids-guide-to-object-oriented-lua-with-corona-sdk . I created simple class Passenger.lua:

local scene = scene -- I have no idea what is it local Passenger = {Instances={}} --end it scene.Passenger = Passenger --STATIC CLASS FUNCTIONS (self refers to the Enemy Class) function Passenger:new() --decorate instance with class attributes, then initialize local instance = setmetatable({}, self) instance:initialize() return instance end --NON-STATIC/ INSTANCE FUNCTIONS (self refers to an enemy instance) function Enemy:dispose() --makes an enemy-in-use available again table.insert(self.Instances, self) end function Enemy:initialize(path) --add to Instances table table.insert(self.Instances, self) end return Passenger

Into my main.lua file I added:

require ( "Passenger" ) local foo = Passenger:new()

and I got error:

…Passenger.lua:2: attempt to index local ‘scene’ (a nil value). When I remove “local scene = scene” and “scene.Passenger = Passenger” I got following error:

“…Passenger.lua:12: attempt to index global ‘Enemy’ (a nil value)”. I know what nil means, but how can I avoid them?

Could enyone tell me, where is error?

Regards,

Michal

Hi,

Problem is, it cannot access the scene, which is created for you if you use Storyboard API. So either make it global or pass it to the class’s constructor.

Hi,

Problem is, it cannot access the scene, which is created for you if you use Storyboard API. So either make it global or pass it to the class’s constructor.