Question

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:

  • A menu with a play button and a highscore button.
  • when tapping on my play button, it's jumping to my game scene (it's always the same game, it doesn't contain any level or so), if loosing then jumping to another scene that looks like, restart or go to menu.
  • when tapping on the highscore button it'll show the best scores.

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.

Was it helpful?

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:

OTHER TIPS

A little recommendation:

Please, don't use Uppercase when naming the root folder of your scenes or it will show you an error message. Just don't use it and all will be fine :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top