Best placement of game elements within the scene phases of Composer

Hello,

I have been making a game engine using the composer API and all has gone quite well so far. But I am starting to clean up my code and I realize now that I am trying to add a “try again” function (something that reloads a level) there may be better ways to structure my code within the composer hierarchy.

I have read some tutorials and articles on composer and I have also looked at the notes that are included with the scene template file but I am still confused as to where is the best place to put particular handlers and code. So I figure I’ll ask those with more experience than I with this API.

I will be focusing on describing my game.lua scene - this is of course where the game loop and all game (non-menu) related events play out.

My first basic set up which worked well but was probably not sustainable/efficient had a majority of my code sitting in the create scene chunk (this included collision, touch and runtime events).

To start, before the scene create chunk, I just initialized all my variables and what not. (I know now that anything to do with screen placement would serve me better in the ‘will’ phase of the scene show chunk)

Now in the scene create chunk I had my:

Level/Game object loading lines (I am using the 3rd party Level Director as well)

This seems to be the correct place to accomplish this

Now I have my touch listeners, my first question is: Should the functions that are handling my touch events be inside or outside the scene create chunk (I currently have them inside and they work nicely there but there is something strange to me about that set up)

ie.

function scene:create( event )

function handleBack (event)

end

back:addEventListener(“tap”,handleBack)

end

should the function that handles these events be included in this scene create chunk or on the other side of that end?

Also what about collision and runtime events? My game worked fine with everything jammed in the create scene junk (all three of these event types)
 

I assume that runtime events would be better suited for the ‘did’ phase of the show scene chunk. but what about collision events?

My main concern is: If I have a DPAD touch object that returns values in which my update runtime method uses to move a character will it still work having my runtime enterFrame event in my scene show chunk and my touch listeners in the scene create chunk? Could scope be an issue?

I want to learn from those with experience, so where did you put your game components such as touch, collision and runtime handlers? In what way did you find it easiest and most effective to have your code divided amongst the many composer scene  phases?

Thanks in advance for any advice or articles that could help!