Hi there
External modules are very useful, but module(…, package.seeall) was deprecated some time ago and also means that in many cases that you have to define functions as global - which isn’t always a good thing to do.
The preferred way to implement external modules is to create a local table within the module that defines all local variables and functions, and then returns the table to be used locally. Jon Beebe wrote a Blog post on it here:
http://blog.anscamobile.com/2011/09/a-better-approach-to-external-modules/
With regards to returning a display group, this is easily done. Here’s an example:
[lua]-- myExternalModule.lua
local M = {}
local _W = display.contentWidth
local _H = display.contentHeight
– ADD BORDERS
local function addBorders()
– Create display group
local myGroup = display.newGroup()
– Create display objects for borders
local ground = display.newRect(0,0,_W,20)
ground.x = _W * 0.5; ground.y = _H - (ground.height * 0.5)
ground:setFillColor(0, 0, 0)
local ceiling = display.newRect(0,0,_W,20);
ceiling.x = _W * 0.5; ceiling.y = 0 - (ceiling.height * 0.5)
ceiling:setFillColor(0, 0, 0)
local leftWall = display.newRect(0,0,10,_H)
leftWall.x = 0 - (leftWall.width * 0.5); leftWall.y = _H * 0.5
leftWall:setFillColor(0, 0, 0)
local rightWall = display.newRect(0,0,10,_H)
rightWall.x = _W + (rightWall.width * 0.5); rightWall.y = _H * 0.5
rightWall:setFillColor(0, 0, 0)
– Insert display objects into group
myGroup:insert( ground )
myGroup:insert( ceiling )
myGroup:insert( leftWall )
myGroup:insert( rightWall )
– Return the group
return myGroup
end
M.addBorders = addBorders
return M[/lua]
Then from your main.lua (or other scene), you could then do this:
[lua]-- main.lua
– Require in myExternalModule.lua
local myExternalModule = require(“myExternalModule”)
– Call the addBorders function which returns the display group (myGroup) and assigns it locally
local borders = myExternalModule.addBorders()[/lua]
Hope that all makes sense
[import]uid: 74503 topic_id: 21260 reply_id: 84223[/import]