luacoronasdkcorona-storyboard

Corona sdk: Scene structure


I searched hours long and still didn't understand.
Here is my problem, I made a tiny game it's working, but now I need a menu, highscores screen and so on. It's meaning that my game is completly made without using storyboards/scenes functions.

This is my structure:

Introduction
In there I put everything that needs to be started at first such as the physics function.

Variables
In there I have all my variables such as:

local object = 0 <or>
local object = display.newObject(param)

Functions
I put in there all my functions such as:

local function functionName (event)
end


Event listeners
In there I have every event listener such as:

functionName:addEventListener("eventType", functionName)


So this is all what I have, what I found in my researches is that I need some functions to create my scene, enter my scene, exit my scene and destroy my scene. The problem is I don't know where to put these scenes and where to put the rest of my code. Basically what I need is:

And what if create objects in my functions? Is it going to disturb the scene? And what about score, I need it when switching scenes for the highscore and so on, is there a way to don't make the score not only local but public?

I hope I'm clear enough and that you will understand what I need! :) As structure example, there is the game called "doodle jump", and my game structure should look a bit the same.


Solution

  • Part of creating good code comes from how you choose to organize and format it.

    This is what I like to do:

    I have a main.lua file + a scenes directory with all my scenes inside. I use a lua file for each scene.

    -> ProjectName/
         - main.lua
         -> scenes/
            - mainMenu.lua
            - highScores.lua
            - game.lua
            - pauseMenu.lua
            - etc ...
    

    main.lua

    Inside main.lua I setup the game and all settings. After the setup I call the mainMenu scene to appear.

    local composer = require("composer")
    
    -- Setup game here:
    composer.mySettings = "Some settings that can be accessed in any scene (highscores, volume settings, etc)"
    composer.myVolume = 100
    
    
    -- Use composer to go to our first game scene
    composer.goToScene("scenes.mainMenu")
    

    scenes/someScene.lua

    Scenes respond to events in corona. These events are:

    scene:create()
    scene:show()
    scene:hide()
    scene:destroy()
    

    Tips

    I recommend you follow this tutorial

    And use this template for all your scenes: