You’re asking this a week or two too early. We are hoping to have a guide for making a simple menu up and going soon.
Any way, you basically need three classes of things to build your menu:
-
Your background image and any other static art work like your Title.
-
Your buttons which may require art work if you’re not using text only buttons.
-
Functions to handle each button, and in most cases it will likely just go to the scene for the button.
Basically copy the template from: https://docs.coronalabs.com/guide/system/composer/index.html#template and create a file called “menu.lua” and paste the code into that template.
The first thing we need to talk about are buttons. Corona SDK can do buttons many different ways. One way is to use the widget.newButton() API call. Another way is to just use a display.newImageRect() or a display.newText() object and put a touch or tap handler on it. I’m going to use a display.newText() object with a “tap” handler as this is perhaps the simplest path.
Since we are going to be using “tap” events to trigger the buttons you need to define your functions to handle them:
In the area just below the comments that reads:
-- ----------------------------------------------------------------------------------- -- Code outside of the scene event functions below will only be executed ONCE unless -- the scene is removed entirely (not recycled) via "composer.removeScene()" -- -----------------------------------------------------------------------------------
write functions to handle each of your buttons. For the purpose of this post, there will be two scenes: game.lua and gamesettings.lua.
local function gotoSettings() composer.gotoScene( "gamesettings" ) end local function playGame() composer.gotoScene( "game" ) end
Next go to the function: function scene:create( event ) and we will add some code there. Just below the line that reads “local sceneGroup = self.view” load your background, title, and create the buttons. Now I’m going to make stuff up. This will not work directly in your code because I don’t know your image names, image sizes, how you have your config.lua setup etc.
local background = display.newImageRect( sceneGroup, "background.png", 360, 570 ) background.x = display.contentCenterX background.y = display.contentCenterY local title = display.newImageRect( sceneGroup, "title.png", 250, 25 ) -- assumes the title is 250x25 title.x = display.contentCenterX title.y = 50 local playButton = display.newText( sceneGroup, "Play!", display.contentCenterX, 160, native.systemFontBold, 20 ) playButton:addEventListener( "tap", playGame ) local gameSettingsButton = display.newText( sceneGroup, "Settings", display.contentCenterX, 200, native.systemFontBold, 20 ) gameSettingsButton:addEventListener( "tap", gotoSettings )
In the simplest of setups, you now have a menu. But you may want to use graphics instead of text. You may like the convenience of the widget.newButton() API call. Taps have to be pretty precise duration and you may want to use touch events instead. You may want to use more buttons/other scenes. You may want a landscape app, the above assumes portrait. You have a lot of decisions to make before you jump straight into the code.
I intentionally did not explain all of the steps and what they are doing. You can look up the docs for each of the API calls to learn about this.
Rob