@Johan,
That is a very unusual organization scheme and I don’t suggest you do that.
-
You’re using globals when you don’t need them.
-
You’re separating the building of objects from the code that operates on them.
-
You don’t have (what I can detect) any clear linkage between code in separate files so you can maintain this later and remember h ow it works.
You’d be better off making modules that contain all of the following:
-
Builder function for creating your objects.
-
Listeners and other functions closely bound to the object.
-
Other support code for the objects and listeners. i.e. Utility code unique to this scope.
If on the other hand you’re trying to build a hierarchical system (not necessarily OOP) where there are multiple object types which share common listeners, etc you could do this:
-- common.lua -- contains commonly used listener for this example. local common = {} common.onTouch( self, event ) if( event.phase == "ended" ) then print(self.myName) end return true end return common
-- builder1.lua -- Builds simple object and uses common.lua listener local common = require "common" local builder = {} builder.create( x, y, name ) local tmp = display.newCircle( x, y, 10 ) tmp.myName = name tmp.touch = common.onTouch tmp.addEventListener( "touch" ) return tmp end return builder
-- builder2.lua -- Builds simple object and uses common.lua listener local common = require "common" local builder = {} builder.create( x, y, name ) local tmp = display.newRect( x, y, 20, 20 ) tmp.myName = name tmp.touch = common.onTouch tmp.addEventListener( "touch" ) return tmp end return builder
-- main.lua -- Simple example bringing it all together local builder1 = require "builder1" local builder2 = require "builder2" builder1.create( 100, 100, "Bill" ) builder2.create( 100, 150, "Bob" ) builder1.create( 200, 100, "Susie" ) builder2.create( 200, 150, "Sally" )