Please tell me if I am using Corona correctly..

Hi all!

I’m making my first game with corona, which is a simple game with main menu, main game, and try again screens. The object of the game will be for the character (a flying frog…) to dodge asteroids that are falling at random x coordinates from the top of the screen. the character can be moved left of right depending on which side of the screen is touched. 

I haven’t gotten too far in the game, but I just wanted to hopefully get some feedback on if I’m doing everything correctly.

I have all code on the main.lua file. I have functions for the background, character, asteroids, etc. set up at the top. Then I have functions for the mainMenu(), mainGame(), and tryAgain() which will use the functions created beforehand. I will then use events like collision or touches in the mainMenu(), mainGame(), and tryAgain() functions to switch between the 3 game states. Is this the correct way to do it in Corona? I keep hearing about storyboards, but no idea what those are.

Here is what the code looks like, it isn’t nearly complete.


-- hide status bar

display.setStatusBar(display.HiddenStatusBar)

-- start physics and turn off gravity

local physics = require("physics")  
physics.start()  
physics.setGravity(0,0)

-- set W and H to width and height of device screen

local W = display.contentWidth  
local H = display.contentHeight

-- table to hold spawned asteroids

local ast={}

-- asteroid properties

local function asteroids()  
   local asteroid = display.newImage("Images/ast.png")  
   asteroid.width = 128  
   asteroid.height = 128  
   asteroid.x = W/2  
   asteroid.y = 0  
   physics.addBody(asteroid, {0,0,0})  
   asteroid:setLinearVelocity(0,600)  
end

-- asteroid spawner (table.insert)

local function astSpawn()

end

-- frog properties

local function frog()  
   local frog = display.newImage("Images/Frog6.png")  
   frog.x = W/2  
   frog.y = H/1.2  
   frog.width = 96  
   frog.height = 128  
end

-- background properties

local function background()  
   local background = display.newImage("Images/Background.png")  
   background.x = W/2  
   background.y = H/2  
   background.width = W  
   background.height = H  
end

-- button properties

local function button()  
   local button = display.newImage("Images/bluebutton.png")  
   button.x = W/2  
   button.y = H/2  
   local text = display.newText("Play Game",W/2,H/2,nativesystemFont,64)  
end

-- main menu screen functions (will have touch event triggering mainGame() to start)

local function mainMenu()  
   background()  
   button()  
   frog()  
end

-- main game screen functions (will have collision event triggering tryAgain() to start)

local function mainGame()  
   background()  
   frog()  
   asteroids()  
end

-- try again screen functions (will have touch event triggering mainMenu() to start)

local function tryAgain()

end

-- starts the game at mainMenu() screen

mainMenu()

Any feedback would be awesome!

Technically there is nothing wrong with putting everything inside main.lua. But as your game grows in complexity it wil be very challenging to have to scroll through hundreds or thousands of lines of code to edit a single file.

For larger projects, you should use main.lua only to initialize things about your game state and then begin a transition to your first scene. This is where Composer comes in (Composer is the next version of Storyboard. Storyboard is no longer being supported.). Composer is a library that helps you manage scenes. For example, you might have a main menu scene, a level selector scene, the actual game play scene, and maybe even a game over scene. Each scene is normally responsible for one thing so you can compartmentalize the logic in each of those files.

So as a very basic example, in your main.lua, you would have something like

local composer = require("composer") local physics = require("physics") physics.start() physics.setGravity(0,0) display.setStatusBar(display.HiddenStatusBar) local function goToMenu()    composer.gotoScene( "mainmenu", "fade", 300) end goToMenu() --call the function above to initiate the transition to main menu

Then inside mainmenu.lua you will add the background for that scene, the buttons that the user can press, and maybe the logo for your game. You’ll set up a function to execute when the user touches a button and that will take you to your next scene.

local function goToLevelSelector()    composer.gotoScene( "levelselector", "fade", 300)    return true end

This just barely scratches the surface but I hope that makes sense! Take a look at the Composer tutorial for more information.

I see, thanks for the info!

It looks like composer is mainly doing the same thing, except it has the aesthetic fade animations. Plus a different tab for each scene to make it easier to keep track of.

Technically there is nothing wrong with putting everything inside main.lua. But as your game grows in complexity it wil be very challenging to have to scroll through hundreds or thousands of lines of code to edit a single file.

For larger projects, you should use main.lua only to initialize things about your game state and then begin a transition to your first scene. This is where Composer comes in (Composer is the next version of Storyboard. Storyboard is no longer being supported.). Composer is a library that helps you manage scenes. For example, you might have a main menu scene, a level selector scene, the actual game play scene, and maybe even a game over scene. Each scene is normally responsible for one thing so you can compartmentalize the logic in each of those files.

So as a very basic example, in your main.lua, you would have something like

local composer = require("composer") local physics = require("physics") physics.start() physics.setGravity(0,0) display.setStatusBar(display.HiddenStatusBar) local function goToMenu()    composer.gotoScene( "mainmenu", "fade", 300) end goToMenu() --call the function above to initiate the transition to main menu

Then inside mainmenu.lua you will add the background for that scene, the buttons that the user can press, and maybe the logo for your game. You’ll set up a function to execute when the user touches a button and that will take you to your next scene.

local function goToLevelSelector()    composer.gotoScene( "levelselector", "fade", 300)    return true end

This just barely scratches the surface but I hope that makes sense! Take a look at the Composer tutorial for more information.

I see, thanks for the info!

It looks like composer is mainly doing the same thing, except it has the aesthetic fade animations. Plus a different tab for each scene to make it easier to keep track of.