start button malfunction!

I am creating the start menu for my app . I am trying to make it so that when you press the play button you start the game ( but in corona’s case go to game.lua file ). this is my code:

local composer = require( “composer” )

– display background image
 local background = display.newImage( “background.png” )
 background.x = 10; background.y = 308
 
 – display ground image
    local ground = display.newImage( “ground.png” )
     ground.x = 145; ground.y = 480
    
    – display logo image
   local logo = display.newImageRect( “logo.png”,  250, 150 )
   logo.x = 160 logo.y = 7
  
   – display play button
   local button = display.newImageRect( “button.png”, 100, 100 )
   button.x = 160 button.y = 350
  
   local function
   onbuttonRelease()
   composer:gotoScene( “game” )
   end
  
   function
   scene:createScene(event)
     local group = self.view
     group:insert(background)
     group:insert(logo)
     group:insert(button)
     group:insert(ground)
     end
     function game:enterScene( game )
     local group = self.view
     end
     function scene:exitScene( game )
      local group = self.view
      composer.removeScene( “main” )
      end
     
      function scene:destroyScene( game )
          local group = self.view
       end
      
scene:addEventListener( “createScene”, scene )

scene:addEventListener( “enterScene”, scene )

scene:addEventListener( “exitScene”, scene )

scene:addEventListener( “destroyScene”, scene )

return scene

:I am getting this message when I relaunch :

main.lua:24:attempt to index global  ‘scene’ (a nil value)

stack traceback

                  main.lua:24:in main chunk

Do you want to restart project ?

:what am I doing wrong what code should I have ? why am I getting this error ?

First of all, you’re code isn’t formatted very well.  In addition when you post code to the forums, use the <> button in the editor to post your code.  Here is your cleaned up code:

local composer = require( "composer" ) local function onbuttonRelease() composer:gotoScene( "game" ) end function scene:createScene(event) local group = self.view -- display background image local background = display.newImage( "background.png" ) background.x = 10; background.y = 308 -- display ground image local ground = display.newImage( "ground.png" ) ground.x = 145; ground.y = 480 -- display logo image local logo = display.newImageRect( "logo.png", 250, 150 ) logo.x = 160 logo.y = 7 -- display play button local button = display.newImageRect( "button.png", 100, 100 ) button.x = 160 button.y = 350 group:insert(background) group:insert(logo) group:insert(button) group:insert(ground) end function game:enterScene( game ) local group = self.view end function scene:exitScene( game ) local group = self.view composer.removeScene( "main" ) end function scene:destroyScene( game ) local group = self.view end scene:addEventListener( "createScene", scene ) scene:addEventListener( "enterScene", scene ) scene:addEventListener( "exitScene", scene ) scene:addEventListener( "destroyScene", scene ) return scene

There are several things wrong.  For instance, in your exitScene function you’re trying to remove “main”.  main.lua is never a scene.  Its an initialization module and a jumping point to your first scene.

Secondly and this is the main problem, you have mixed a composer and a storyboard scene.  The events like enterScene, exitScene, destroyScene, createScene are “Storyboard”.  In composer they are scene:create, scene:show, scene:hide and scene:destroy.  Next, while  you require “composer”, you never actually create the scene.

I would recommend that you read any of our tutorials, documentation or guides.  They all provide a scene template that should be used to base your scenes on.  You should not change any of the core pieces but add your code to it.  In other words don’t remove the code that creates the scene.   In fact, please read this tutorial:

https://coronalabs.com/blog/2015/04/14/tutorial-the-basic-game-template/

There is a link to a basic game template on our GitHub repository.  This template shows you exactly how to use Composer correctly.  Composer will not work for you if you don’t use it the way it’s intended.

Rob

Thank you rob you have been such a help to me when making my app . I wish you the best.

this didn’t work . I tweaked the code a bit . this is the code :

<local composer = require( “composer” )

local function onbuttonRelease()
   composer:gotoScene( “game” )
end
 
gloal = function (scene:)createScene(event)
 local group = self.view
 
 – display background image
  local background = display.newImage( “background.png” )
  background.x = 10; background.y = 308
 
  – display ground image
 local ground = display.newImage( “ground.png” )
 ground.x = 145; ground.y = 480
 
 – display logo image
    local logo = display.newImageRect( “logo.png”,  250, 150 )
    logo.x = 160 logo.y = 7
 
    – display play button
    local button = display.newImageRect( “button.png”, 100, 100 )
    button.x = 160 button.y = 350
 

 group:insert(background)
 group:insert(logo)
 group:insert(button)
 group:insert(ground)
end

function game:enterScene( game )
 local group = self.view
end

function scene:exitScene( game )
 local group = self.view
 composer.removeScene( “main” )
end
 
function scene:destroyScene( game )
 local group = self.view
end
  
scene:addEventListener( “createScene”, scene )
scene:addEventListener( “enterScene”, scene )
scene:addEventListener( “exitScene”, scene )
scene:addEventListener( “destroyScene”, scene )

return scene>

: I am getting this error :

main.lua:7: ‘)’ expected near ‘:’

do you want to relaunch project ? why am I getting this ?

First of all, you’re code isn’t formatted very well.  In addition when you post code to the forums, use the <> button in the editor to post your code.  Here is your cleaned up code:

local composer = require( "composer" ) local function onbuttonRelease() composer:gotoScene( "game" ) end function scene:createScene(event) local group = self.view -- display background image local background = display.newImage( "background.png" ) background.x = 10; background.y = 308 -- display ground image local ground = display.newImage( "ground.png" ) ground.x = 145; ground.y = 480 -- display logo image local logo = display.newImageRect( "logo.png", 250, 150 ) logo.x = 160 logo.y = 7 -- display play button local button = display.newImageRect( "button.png", 100, 100 ) button.x = 160 button.y = 350 group:insert(background) group:insert(logo) group:insert(button) group:insert(ground) end function game:enterScene( game ) local group = self.view end function scene:exitScene( game ) local group = self.view composer.removeScene( "main" ) end function scene:destroyScene( game ) local group = self.view end scene:addEventListener( "createScene", scene ) scene:addEventListener( "enterScene", scene ) scene:addEventListener( "exitScene", scene ) scene:addEventListener( "destroyScene", scene ) return scene

There are several things wrong.  For instance, in your exitScene function you’re trying to remove “main”.  main.lua is never a scene.  Its an initialization module and a jumping point to your first scene.

Secondly and this is the main problem, you have mixed a composer and a storyboard scene.  The events like enterScene, exitScene, destroyScene, createScene are “Storyboard”.  In composer they are scene:create, scene:show, scene:hide and scene:destroy.  Next, while  you require “composer”, you never actually create the scene.

I would recommend that you read any of our tutorials, documentation or guides.  They all provide a scene template that should be used to base your scenes on.  You should not change any of the core pieces but add your code to it.  In other words don’t remove the code that creates the scene.   In fact, please read this tutorial:

https://coronalabs.com/blog/2015/04/14/tutorial-the-basic-game-template/

There is a link to a basic game template on our GitHub repository.  This template shows you exactly how to use Composer correctly.  Composer will not work for you if you don’t use it the way it’s intended.

Rob

Thank you rob you have been such a help to me when making my app . I wish you the best.

this didn’t work . I tweaked the code a bit . this is the code :

<local composer = require( “composer” )

local function onbuttonRelease()
   composer:gotoScene( “game” )
end
 
gloal = function (scene:)createScene(event)
 local group = self.view
 
 – display background image
  local background = display.newImage( “background.png” )
  background.x = 10; background.y = 308
 
  – display ground image
 local ground = display.newImage( “ground.png” )
 ground.x = 145; ground.y = 480
 
 – display logo image
    local logo = display.newImageRect( “logo.png”,  250, 150 )
    logo.x = 160 logo.y = 7
 
    – display play button
    local button = display.newImageRect( “button.png”, 100, 100 )
    button.x = 160 button.y = 350
 

 group:insert(background)
 group:insert(logo)
 group:insert(button)
 group:insert(ground)
end

function game:enterScene( game )
 local group = self.view
end

function scene:exitScene( game )
 local group = self.view
 composer.removeScene( “main” )
end
 
function scene:destroyScene( game )
 local group = self.view
end
  
scene:addEventListener( “createScene”, scene )
scene:addEventListener( “enterScene”, scene )
scene:addEventListener( “exitScene”, scene )
scene:addEventListener( “destroyScene”, scene )

return scene>

: I am getting this error :

main.lua:7: ‘)’ expected near ‘:’

do you want to relaunch project ? why am I getting this ?