Button error

Hello i have a simple question im using some base code in the pre made projects as well as one of the api’s for the code the code im getting. Any assistance would be helpful as i am still learning lua and coronas api’s

(File: ? Attempt to concatenate global ‘sceneName’ (a nil value) 

 

stack traceback:

?: in function ‘gotoScene’

main.lua:14: in main chunk)

the code im using

–MENU–

[lua]-----------------------------------------------------------------------------------------

– menu.lua


local composer = require( “composer” )
local scene = composer.newScene()

– include Corona’s “widget” library
local widget = require “widget”


– forward declarations and other locals
local playBtn

– ‘onRelease’ event listener for playBtn
local function onPlayBtnRelease()

– go to level1.lua scene
composer.gotoScene( “level1”, “fade”, 500 )

return true – indicates successful touch
end

function scene:create( event )
local sceneGroup = self.view

– Called when the scene’s view does not exist.

– INSERT code here to initialize the scene
– e.g. add display objects to ‘sceneGroup’, add touch listeners, etc.

– display a background image
local background = display.newImageRect( “background.jpg”, display.contentWidth, display.contentHeight )
background.anchorX = 0
background.anchorY = 0
background.x, background.y = 0, 0

– create/position logo/title image on upper-half of the screen
local titleLogo = display.newImageRect( “logo.png”, 264, 42 )
titleLogo.x = display.contentWidth * 0.5
titleLogo.y = 100

– create a widget button (which will loads level1.lua on release)
playBtn = widget.newButton{
label=“Play Now”,
labelColor = { default={255}, over={128} },
default=“button.png”,
over=“button-over.png”,
width=154, height=40,
onRelease = onPlayBtnRelease – event listener function
}
playBtn.x = display.contentWidth*0.5
playBtn.y = display.contentHeight - 125

– all display objects must be inserted into group
sceneGroup:insert( background )
sceneGroup:insert( titleLogo )
sceneGroup:insert( playBtn )
end

function scene:show( event )
local sceneGroup = self.view
local phase = event.phase

if phase == “will” then
– Called when the scene is still off screen and is about to move on screen
elseif phase == “did” then
– Called when the scene is now on screen

– INSERT code here to make the scene come alive
– e.g. start timers, begin animation, play audio, etc.
end
end

function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase

if event.phase == “will” then
– Called when the scene is on screen and is about to move off screen

– INSERT code here to pause the scene
– e.g. stop timers, stop animation, unload sounds, etc.)
elseif phase == “did” then
– Called when the scene is now off screen
end
end

function scene:destroy( event )
local sceneGroup = self.view

– Called prior to the removal of scene’s “view” (sceneGroup)

– INSERT code here to cleanup the scene
– e.g. remove display objects, remove touch listeners, save state, etc.

if playBtn then
playBtn:removeSelf() – widgets must be manually removed
playBtn = nil
end
end


– Listener setup
scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )


return scene[/lua]

–LEVEL 1–

[lua]
local composer = require( “composer” )
local scene = composer.newScene()

local widget = require( “widget” )

– Function to handle button events
local function handleButtonEvent( event )

if ( “ended” == event.phase ) then
print( “Button was pressed and released” )
end
end

– Create the widget
local button1 = widget.newButton
{
left = 100,
top = 200,
id = “button1”,
label = “Default”,
onEvent = handleButtonEvent
}
[/lua]

Your scene, level1 isn’t a scene. It’s missing the required “return scene” at the end. It’s also missing the events to handle scene:create(), scene:show(), etc.

Rob

edited code still giving me the same error

[lua]–
– level1.lua


local composer = require( “composer” )
local scene = composer.newScene()

local widget = require( “widget” )

– Function to handle button events
function scene:create( event )
local sceneGroup = self.view
local function handleButtonEvent( event )

if ( “ended” == event.phase ) then
print( “Button was pressed and released” )
end
end

– Create the widget
local button1 = widget.newButton
{
left = 100,
top = 200,
id = “button1”,
label = “Default”,
onEvent = handleButtonEvent
}
end

function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if phase == “will” then
– Called when the scene is still off screen and is about to move on screen
elseif phase == “did” then
– Called when the scene is now on screen

– INSERT code here to make the scene come alive
– e.g. start timers, begin animation, play audio, etc.
end
end[/lua]

You’re still missing:

scene:addEventListener("create") scene:addEventListener("show") return scene

At the bottom of your code

add the functions scene:hide() and scene:destroy to level1.lua and then put return scene at the bottom

Also add the scene listeners just put them right above return scene

  1. scene:addEventListener( “create”, scene )
  2. scene:addEventListener( “show”, scene )
  3. scene:addEventListener( “hide”, scene )
  4. scene:addEventListener( “destroy”, scene )

these are all the coded files i did the editing and i proceed to get this error

–ERROR–

File: ?

Attempt to concatenate global ‘sceneName’ (a nil value)

 

stack traceback:

?: in function ‘gotoScene’

main.lua:14: in main chunk

 

–LEVEL 1–

 

[lua]-----------------------------------------------------------------------------------------

– level1.lua


local composer = require( “composer” )
local scene = composer.newScene()

local widget = require( “widget” )

– Function to handle button events
function scene:create( event )
local sceneGroup = self.view
local function handleButtonEvent( event )

if ( “ended” == event.phase ) then
print( “Button was pressed and released” )
end
end

– Create the widget
local button1 = widget.newButton
{
left = 100,
top = 200,
id = “button1”,
label = “Default”,
onEvent = handleButtonEvent
}
end

function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if phase == “will” then
– Called when the scene is still off screen and is about to move on screen
elseif phase == “did” then
– Called when the scene is now on screen

– INSERT code here to make the scene come alive
– e.g. start timers, begin animation, play audio, etc.
end
end
function scene:hide( event )
print( event.name )
end

function scene:destroy( event )
print( event.name )
end

scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )

return scene[/lua]

 

–MAIN–

 

[lua]-----------------------------------------------------------------------------------------

– main.lua


– hide the status bar
display.setStatusBar( display.HiddenStatusBar )

– include the Corona “composer” module
local composer = require “composer”

– load menu screen
composer.gotoScene( “menu” )[/lua]

 

–MENU–

 

[lua]-----------------------------------------------------------------------------------------

– menu.lua


local composer = require( “composer” )
local scene = composer.newScene()

– include Corona’s “widget” library
local widget = require “widget”


local widget = require( “widget” )

– Function to handle button events
local function handleButtonEvent( event )

if ( “ended” == event.phase ) then
print( “Button was pressed and released” )
end
end

– Create the widget
local button1 = widget.newButton
{
left = 100,
top = 200,
id = “button1”,
label = “Default”,
onEvent = handleButtonEvent
}
[/lua]

Your menu screen is still incorrect.

The comment Rob made applied to that scene also. Make the changes he suggested to your menu screen as well, and everything will be fine.

(Add scene handlers, scene listeners and return scene at the end of the file)

And make sure to insert your widgets and display objects into sceneGroup!!!

Rob

Your scene, level1 isn’t a scene. It’s missing the required “return scene” at the end. It’s also missing the events to handle scene:create(), scene:show(), etc.

Rob

edited code still giving me the same error

[lua]–
– level1.lua


local composer = require( “composer” )
local scene = composer.newScene()

local widget = require( “widget” )

– Function to handle button events
function scene:create( event )
local sceneGroup = self.view
local function handleButtonEvent( event )

if ( “ended” == event.phase ) then
print( “Button was pressed and released” )
end
end

– Create the widget
local button1 = widget.newButton
{
left = 100,
top = 200,
id = “button1”,
label = “Default”,
onEvent = handleButtonEvent
}
end

function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if phase == “will” then
– Called when the scene is still off screen and is about to move on screen
elseif phase == “did” then
– Called when the scene is now on screen

– INSERT code here to make the scene come alive
– e.g. start timers, begin animation, play audio, etc.
end
end[/lua]

You’re still missing:

scene:addEventListener("create") scene:addEventListener("show") return scene

At the bottom of your code

add the functions scene:hide() and scene:destroy to level1.lua and then put return scene at the bottom

Also add the scene listeners just put them right above return scene

  1. scene:addEventListener( “create”, scene )
  2. scene:addEventListener( “show”, scene )
  3. scene:addEventListener( “hide”, scene )
  4. scene:addEventListener( “destroy”, scene )

these are all the coded files i did the editing and i proceed to get this error

–ERROR–

File: ?

Attempt to concatenate global ‘sceneName’ (a nil value)

 

stack traceback:

?: in function ‘gotoScene’

main.lua:14: in main chunk

 

–LEVEL 1–

 

[lua]-----------------------------------------------------------------------------------------

– level1.lua


local composer = require( “composer” )
local scene = composer.newScene()

local widget = require( “widget” )

– Function to handle button events
function scene:create( event )
local sceneGroup = self.view
local function handleButtonEvent( event )

if ( “ended” == event.phase ) then
print( “Button was pressed and released” )
end
end

– Create the widget
local button1 = widget.newButton
{
left = 100,
top = 200,
id = “button1”,
label = “Default”,
onEvent = handleButtonEvent
}
end

function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if phase == “will” then
– Called when the scene is still off screen and is about to move on screen
elseif phase == “did” then
– Called when the scene is now on screen

– INSERT code here to make the scene come alive
– e.g. start timers, begin animation, play audio, etc.
end
end
function scene:hide( event )
print( event.name )
end

function scene:destroy( event )
print( event.name )
end

scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )

return scene[/lua]

 

–MAIN–

 

[lua]-----------------------------------------------------------------------------------------

– main.lua


– hide the status bar
display.setStatusBar( display.HiddenStatusBar )

– include the Corona “composer” module
local composer = require “composer”

– load menu screen
composer.gotoScene( “menu” )[/lua]

 

–MENU–

 

[lua]-----------------------------------------------------------------------------------------

– menu.lua


local composer = require( “composer” )
local scene = composer.newScene()

– include Corona’s “widget” library
local widget = require “widget”


local widget = require( “widget” )

– Function to handle button events
local function handleButtonEvent( event )

if ( “ended” == event.phase ) then
print( “Button was pressed and released” )
end
end

– Create the widget
local button1 = widget.newButton
{
left = 100,
top = 200,
id = “button1”,
label = “Default”,
onEvent = handleButtonEvent
}
[/lua]

Your menu screen is still incorrect.

The comment Rob made applied to that scene also. Make the changes he suggested to your menu screen as well, and everything will be fine.

(Add scene handlers, scene listeners and return scene at the end of the file)

And make sure to insert your widgets and display objects into sceneGroup!!!

Rob