Hi, I use Corona for a short time , I’m studying this framework by creating small games and solving the problems that encounter. My programming approach is procedural, I don’t use class, ecc…
I did some research on the web and on the forums, read some manuals, but I am not yet clear on the proper use of the modules.
In my projects I use Composer to divide the game into scenes. In addition to tidy the projects and not to have scene lua files long thousands of line, I got used to group the functions used by each scene into some modules.
At the beginning of each scene I use “require” to call modules. The modules do not contain more that the functions one after the other.
I’ve noticed that I can not hold functions as a local within the modules but actually have to be global.
I can’t put local functions in modules? What is the correct way to use modules? I have to remove module in scene:destroy or it is done automatically?
Example:
[lua]
–
– menu.lua
local composer = require( “composer” )
local scene = composer.newScene()
require(“Example”)
function scene:create( event )
local sceneGroup = self.view
– Called when the scene’s view does not exist.
– INSERT code here to initialize the scene
– e.g. add display objects to ‘sceneGroup’, add touch listeners, etc.
end
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if phase == “will” then
– Called when the scene is still off screen and is about to move on screen
elseif phase == “did” then
– Called when the scene is now on screen
– INSERT code here to make the scene come alive
– e.g. start timers, begin animation, play audio, etc.
–//ALL THIS FUNCTIONS ARE IN Example MODULE//–
gameExample()
example()
exampleSwipe()
exampleTilt()
end
end
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if event.phase == “will” then
– Called when the scene is on screen and is about to move off screen
– INSERT code here to pause the scene
– e.g. stop timers, stop animation, unload sounds, etc.)
elseif phase == “did” then
– Called when the scene is now off screen
end
end
function scene:destroy( event )
local sceneGroup = self.view
– Called prior to the removal of scene’s “view” (sceneGroup)
– INSERT code here to cleanup the scene
– e.g. remove display objects, remove touch listeners, save state, etc.
sceneGroup:removeSelf( )
sceneGroup=nil
end
– Listener setup
scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )
return scene
[/lua]
and this is Example module:
[lua]
function gameExample()
–CODE–
end
function example()
–CODE–
end
function exampleSwipe()
–CODE–
end
function exampleTilt()
–CODE–
end
[/lua]
Sorry for Google Translate