Thanks Antheor!
The new template is not so different of the other, it’s just separated in “blocks”. Let’s go block-by-block.
1 - GROUPS
Here you can declare all groups that you will be using on the scene. Imagine if you have a game scene and you have a pause screen. You can divide them into 2 groups. All objects of the game you have to insert into the gameGroup and the objects of the pause menu into the pauseGroup. Also, these new groups must be inserted into the localGroup.
---------------------------------------------------------------
-- GROUPS
---------------------------------------------------------------
local localGroup = display.newGroup()
local gameGroup = display.newGroup()
local pauseGroup = display.newGroup()
2 - DISPLAY OBJECTS
Here you can declare all your images, animations, buttons and vectors. Why declaring them here? Just because we will declare our functions after this and with this prevent referencing null pointers. This is what happen when you declare a function before the variable, Corona will not recognize the variable because it doesn’t exists when the function was created.
---------------------------------------------------------------
-- DISPLAY OBJECTS
---------------------------------------------------------------
local background = display.newRect(0,0,320,480)
local player = display.newImage("player.png")
3 - LISTENERS
This is just where I put all my functions that will be added to graphics as listeners using the command addEventListener. Just remember to put the functions in order to prevent null pointers as I told in the section 2 here.
---------------------------------------------------------------
-- LISTENERS
---------------------------------------------------------------
local function touched ( event )
if event.phase == "ended" then
local showFx = transition.to ( player, {x=event.x, y=event.y, time=500} )
end
end
4 - INIT VARS
This is just to organize everything on the screen, setup colors and add/remove the listeners to begin the level. When you want to restart a game, you want to everything be as when you load the scene, ok? That’s is what this function have to do.
---------------------------------------------------------------
-- INIT VARS
---------------------------------------------------------------
local function initVars ()
-----------------------------------
-- Inserts
-----------------------------------
gameGroup(background)
localGroup:insert(gameGroup)
5 - CLEAN
Here you can stop timers, remove listeners and all other things you want to do before changing the scene to prevent errors. You don’t need this but if you have, Director will call it from the changeScene function.
---------------------------------------------------------------
-- CLEAN
---------------------------------------------------------------
function clean ( event )
Runtime:removeEventListener ( "enterFrame" , gameTime )
end
6 - NEW
Well, I don’t have to say anything about this function, you all know what it is and how to use it. Just never forget to return the localGroup.
[code]
– NEW
function new()
– Initiate variables
initVars()
– MUST return a display.newGroup()
return localGroup
end
[/code] [import]uid: 8556 topic_id: 5872 reply_id: 23004[/import]