How can I remove these DisplayObjects at end of a scene? - lua

I want the balloons in this scene, which are DisplayObjects, to disappear when the scene ends. I have added them to the local sceneGroup = scene.view by passing this as the first argument to display.newImageRect() and assumed this would be enough for them to be removed as they are listed in the destroy.scene bit at the bottom. However when it transitions to the next scene they are still there. Why are these DisplayObjects not being removed?
local composer = require( "composer" )
local scene = composer.newScene()
local Balloons
local positioninsheetOptions = 100
local sheetOptions =
{
frames =
{
{
x = 0,
y = 0,
width = 71,
height = 100
},
{
x = 0,
y = positioninsheetOptions,
width = 71,
height = 100
},
{
x = 0,
y = positioninsheetOptions*2,
width = 71,
height = 100
},
{
x = 0,
y = positioninsheetOptions*3,
width = 71,
height = 100
},
{
x = 0,
y = positioninsheetOptions*4,
width = 71,
height = 100
},
{
x = 0,
y = positioninsheetOptions*5,
width = 71,
height = 100
},
{
x = 0,
y = positioninsheetOptions*6,
width = 71,
height = 100
},
{
x = 0,
y = positioninsheetOptions*7,
width = 71,
height = 100
},
{
x = 0,
y = positioninsheetOptions*8,
width = 71,
height = 100
},
{
x = 0,
y = positioninsheetOptions*9,
width = 71,
height = 100
},
{
x = 0,
y = positioninsheetOptions*10,
width = 71,
height = 100
},
{
x = 0,
y = positioninsheetOptions*11,
width = 71,
height = 100
},
},
}
local objectSheet = graphics.newImageSheet( "gameObjects.png", sheetOptions )
Balloons = {}
-- -----------------------------------------------------------------------------------
-- Code outside of the scene event functions below will only be executed ONCE unless
-- the scene is removed entirely (not recycled) via "composer.removeScene()"
-- -----------------------------------------------------------------------------------
local function gotoGame()
composer.gotoScene( "game" )
end
-- -----------------------------------------------------------------------------------
-- Scene event functions
-- -----------------------------------------------------------------------------------
-- create()
function scene:create( event )
local sceneGroup = self.view
-- Code here runs when the scene is first created but has not yet appeared on screen
local background = display.newImageRect( sceneGroup, "background.png", 800, 1400 )
background.x = display.contentCenterX
background.y = display.contentCenterY
local balloonBlue1 = display.newImageRect(sceneGroup, objectSheet, 6, 71, 100)
local balloonYellow1 = display.newImageRect(sceneGroup, objectSheet, 8, 71, 100)
local balloonRed1 = display.newImageRect(sceneGroup, objectSheet, 10, 71, 100)
local balloonBlue2 = display.newImageRect(sceneGroup, objectSheet, 6, 71, 100)
local Balloons = display.newGroup()
Balloons:insert( balloonBlue1 )
Balloons:insert( balloonYellow1 )
Balloons:insert( balloonRed1 )
Balloons:insert( balloonBlue2 )
Balloons:addEventListener( "tap", gotoGame )
balloonBlue1.x = (display.contentWidth/8)
balloonBlue1.y = balloonBlue1.height
balloonYellow1.x = (display.contentWidth/8)*3
balloonYellow1.y = balloonBlue1.height
balloonRed1.x = (display.contentWidth/8)*5
balloonRed1.y = balloonBlue1.height
balloonBlue2.x = (display.contentWidth/8)*7
balloonBlue2.y = balloonBlue1.height
end
-- show()
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Code here runs when the scene is still off screen (but is about to come on screen)
elseif ( phase == "did" ) then
-- Code here runs when the scene is entirely on screen
end
end
-- hide()
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
elseif ( phase == "did" ) then
-- Code here runs immediately after the scene goes entirely off screen
end
end
-- destroy()
function scene:destroy( event )
local sceneGroup = self.view
-- Code here runs prior to the removal of scene's view
end
-- -----------------------------------------------------------------------------------
-- Scene event function listeners
-- -----------------------------------------------------------------------------------
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- -----------------------------------------------------------------------------------
return scene

You need to add the Balloon group (of which the balloons are children) to your sceneGroup.
In scene:destroy, make sure you remove any listeners and cancel any transitions on the things you want destroyed. If you have, when the scene's view is destroyed, the Balloon group and all it's children will also be destroyed.

Related

AddEventListener: Listener cannot be nil

Can someone please explain what is going wrong here? It went wrong as soon as I added the AddEventListener
newBalloon:addEventListener( "tap", pushBalloon )
Complete Code:
local composer = require( "composer" )
local scene = composer.newScene()
local physics = require( "physics" )
physics.start()
-- Configure image sheet
local positioninsheetOptions = 144.1
local sheetOptions =
{
frames =
{
{
x = 0,
y = 0,
width = 112,
height = 142
},
{
x = 0,
y = positioninsheetOptions,
width = 112,
height = 142
},
{
x = 0,
y = positioninsheetOptions*2,
width = 112,
height = 142
},
{
x = 0,
y = positioninsheetOptions*3,
width = 112,
height = 142
},
{
x = 0,
y = positioninsheetOptions*4,
width = 112,
height = 142
},
{
x = 0,
y = positioninsheetOptions*5,
width = 112,
height = 142
},
{
x = 0,
y = positioninsheetOptions*6,
width = 112,
height = 142
},
{
x = 0,
y = positioninsheetOptions*7,
width = 112,
height = 142
},
{
x = 0,
y = positioninsheetOptions*8,
width = 112,
height = 142
},
{
x = 0,
y = positioninsheetOptions*9,
width = 112,
height = 142
},
{
x = 0,
y = positioninsheetOptions*10,
width = 112,
height = 142
},
},
}
local objectSheet = graphics.newImageSheet( "gameObjects.png", sheetOptions )
local tapCount = 0
local platform
local tapText
local balloonsTable = {}
local leftBorder
local rightBorder
local backGroup
local mainGroup
local uiGroup
local platform
local platform2
local function createBalloon()
local randomBalloon = math.random( 10 )
local newBalloon = display.newImageRect( objectSheet, randomBalloon, 112, 142 )
if newBalloon then
table.insert( balloonsTable, newBalloon )
physics.addBody( newBalloon, "dynamic", { radius=70, bounce=0 } )
newBalloon.myName = "bigBalloon"
newBalloon.alpha = 0.75
newBalloon.gravityScale = randomBalloon/-150
newBalloon:addEventListener( "tap", pushBalloon )
end
local whereFrom = math.random( 3 )
if ( whereFrom == 1 ) then
-- From the left
newBalloon.x = 100
newBalloon.y = display.contentHeight+300
elseif ( whereFrom == 2 ) then
-- From the top
newBalloon.x = 160
newBalloon.y = display.contentHeight+300
elseif ( whereFrom == 3 ) then
-- From the right
newBalloon.x = 220
newBalloon.y = display.contentHeight+300
end
end
local function gameLoop()
-- Create new balloon
createBalloon()
-- Remove balloons which have drifted off screen
for i = #balloonsTable, 1, -1 do
local thisBalloon = balloonsTable[i]
if ( thisBalloon.x < -100 or
thisBalloon.x > display.contentWidth + 100 or
thisBalloon.y < -100 )
then
display.remove( thisBalloon )
table.remove( balloonsTable, i )
end
end
end
local function pushBalloon( event )
local tappedBalloon = event.target
-- balloon:applyLinearImpulse( 0.2, -2, balloon.x, balloon.y )
-- tapCount = tapCount + 1
-- tapText.text = tapCount
if event.phase == "began" then
tappedBalloon.gravityScale = 10
end
end
local function pushBalloon2()
-- balloon:applyLinearImpulse( 0.2, -2, balloon.x, balloon.y )
-- tapCount = tapCount + 1
-- tapText.text = tapCount
balloon2.gravityScale = 10
balloon2:applyLinearImpulse( 0.1, 0, balloon2.x, balloon2.y )
end
local function pushBalloon3()
-- balloon:applyLinearImpulse( 0.2, -2, balloon.x, balloon.y )
-- tapCount = tapCount + 1
-- tapText.text = tapCount
balloon3.gravityScale = 10
balloon3:applyLinearImpulse( -0.1, 0, balloon3.x, balloon3.y )
end
-- -----------------------------------------------------------------------------------
-- Scene event functions
-- -----------------------------------------------------------------------------------
-- create()
function scene:create( event )
local sceneGroup = self.view
-- Code here runs when the scene is first created but has not yet appeared on screen
physics.pause()
local background = display.newImageRect( "background.png", 360, 570 )
background.x = display.contentCenterX
background.y = display.contentCenterY
platform = display.newImageRect( "platform.png", 10, display.contentHeight*5 )
platform.x = -5
platform.y = 0
physics.addBody( platform, "static", { friction=0.5, bounce=0.3 } )
platform2 = display.newImageRect( "platform.png", 10, display.contentHeight*5 )
platform2.x = display.contentWidth+5
platform2.y = 0
physics.addBody( platform2, "static", { friction=0.5, bounce=0.3 } )
createBalloon()
end
-- show()
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Code here runs when the scene is still off screen (but is about to come on screen)
elseif ( phase == "did" ) then
gameLoopTimer = timer.performWithDelay( 1250, gameLoop, 0 )
-- Code here runs when the scene is entirely on screen
physics.start()
end
end
-- hide()
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Code here runs when the scene is on screen (but is about to go off screen)
elseif ( phase == "did" ) then
-- Code here runs immediately after the scene goes entirely off screen
physics.pause()
end
end
-- destroy()
function scene:destroy( event )
local sceneGroup = self.view
-- Code here runs prior to the removal of scene's view
end
-- -----------------------------------------------------------------------------------
-- Scene event function listeners
-- -----------------------------------------------------------------------------------
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- -----------------------------------------------------------------------------------
return scene
Thank you.
You refer to function (pushBalloon) do not exist yet when add event listener. So put definition of function above line you add event listener like that
local function pushBalloon( event )
local tappedBalloon = event.target
-- balloon:applyLinearImpulse( 0.2, -2, balloon.x, balloon.y )
-- tapCount = tapCount + 1
-- tapText.text = tapCount
if event.phase == "began" then
tappedBalloon.gravityScale = 10
end
end
...
newBalloon:addEventListener( "tap", pushBalloon )
At the point in the file where you are adding the listener, pushBalloon() is not defined. You should add a forward declaration earlier in the file:
local scene = ...
local pushBalloon
...
-- createBalloon() and other existing code
...
pushBalloon = function ( event )
-- your function code
end

Expecting a table?

I started learning Corona yesterday so sorry for my ignorance. Can someone please explain why it is "expecting table" on line 108 (physics.addBody( newBalloon, "dynamic", { radius=70, bounce=0.8 } )). All I want it to do is take the image from the object sheet and display it.
local composer = require( "composer" )
local scene = composer.newScene()
local physics = require( "physics" )
physics.start()
-- Configure image sheet
local sheetOptions =
{
frames =
{
{
x = 0,
y = 0,
width = 112,
height = 142
},
{
x = 0,
y = 142,
width = 112,
height = 284
},
{
x = 0,
y = 284,
width = 112,
height = 568
},
{
x = 0,
y = 568,
width = 112,
height = 710
},
{
x = 0,
y = 710,
width = 112,
height = 852
},
{
x = 0,
y = 852,
width = 112,
height = 994
},
{
x = 0,
y = 994,
width = 112,
height = 1136
},
{
x = 0,
y = 1136,
width = 112,
height = 1278
},
{
x = 0,
y = 1278,
width = 112,
height = 1420
},
},
}
local objectSheet = graphics.newImageSheet( "gameObjects.png", sheetOptions )
local tapCount = 0
local platform
local balloon
local balloon2
local balloon3
local tapText
local function createBalloon()
newBalloon = display.newImageRect( mainGroup, objectSheet, 1, 112, 142 )
physics.addBody( newBalloon, "dynamic", { radius=70, bounce=0.8 } )
newBalloon.myName = "Bigballoon"
local whereFrom = math.random( 3 )
if ( whereFrom == 1 ) then
-- From the left
newBalloon.x = 30
newBalloon.y = math.random( display.contentHeight )
elseif ( whereFrom == 2 ) then
-- From the top
newBalloon.x = 60
newBalloon.y = math.random( display.contentHeight )
elseif ( whereFrom == 3 ) then
-- From the right
newBalloon.x = 90
newBalloon.y = math.random( display.contentHeight )
end
end
local function pushBalloon()
-- balloon:applyLinearImpulse( 0.2, -2, balloon.x, balloon.y )
-- tapCount = tapCount + 1
-- tapText.text = tapCount
balloon.gravityScale = 10
balloon:applyLinearImpulse( 0.1, 0, balloon.x, balloon.y )
end
local function pushBalloon2()
-- balloon:applyLinearImpulse( 0.2, -2, balloon.x, balloon.y )
-- tapCount = tapCount + 1
-- tapText.text = tapCount
balloon2.gravityScale = 10
balloon2:applyLinearImpulse( 0.1, 0, balloon2.x, balloon2.y )
end
local function pushBalloon3()
-- balloon:applyLinearImpulse( 0.2, -2, balloon.x, balloon.y )
-- tapCount = tapCount + 1
-- tapText.text = tapCount
balloon3.gravityScale = 10
balloon3:applyLinearImpulse( -0.1, 0, balloon3.x, balloon3.y )
end
-- -----------------------------------------------------------------------------------
-- Scene event functions
-- -----------------------------------------------------------------------------------
-- create()
function scene:create( event )
local sceneGroup = self.view
-- Code here runs when the scene is first created but has not yet appeared on screen
physics.pause()
local background = display.newImageRect( "background.png", 360, 570 )
background.x = display.contentCenterX
background.y = display.contentCenterY
local platform = display.newImageRect( "platform.png", 300, 50 )
platform.x = display.contentCenterX
platform.y = display.contentHeight-25
balloon = display.newImageRect( "balloon.png", 112, 112 )
balloon.x = display.contentCenterX
balloon.y = display.contentCenterY
balloon.alpha = 0.8
balloon2 = display.newImageRect( "balloon.png", 112, 112 )
balloon2.x = display.contentCenterX-100
balloon2.y = display.contentCenterY
balloon2.alpha = 0.8
balloon3 = display.newImageRect( "balloon.png", 112, 112 )
balloon3.x = display.contentCenterX+100
balloon3.y = display.contentCenterY
balloon3.alpha = 0.8
createBalloon()
tapText = display.newText( tapCount, display.contentCenterX, 20, native.systemFont, 40 )
tapText:setFillColor( 0, 0, 0 )
physics.addBody( platform, "static" )
physics.addBody( balloon, "dynamic", { radius=50, bounce=0.6 } )
physics.addBody( balloon2, "dynamic", { radius=50, bounce=0.6 } )
physics.addBody( balloon3, "dynamic", { radius=50, bounce=0.6 } )
balloon:addEventListener( "tap", pushBalloon )
balloon.gravityScale = 0
balloon:setLinearVelocity( 0, -10 )
balloon2:addEventListener( "tap", pushBalloon2 )
balloon2.gravityScale = 0
balloon2:setLinearVelocity( 0, -10 )
balloon3:addEventListener( "tap", pushBalloon3 )
balloon3.gravityScale = 0
balloon3:setLinearVelocity( 0, -10 )
end
-- show()
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Code here runs when the scene is still off screen (but is about to come on screen)
elseif ( phase == "did" ) then
-- Code here runs when the scene is entirely on screen
physics.start()
end
end
-- hide()
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Code here runs when the scene is on screen (but is about to go off screen)
elseif ( phase == "did" ) then
-- Code here runs immediately after the scene goes entirely off screen
physics.pause()
end
end
-- destroy()
function scene:destroy( event )
local sceneGroup = self.view
-- Code here runs prior to the removal of scene's view
end
-- -----------------------------------------------------------------------------------
-- Scene event function listeners
-- -----------------------------------------------------------------------------------
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- -----------------------------------------------------------------------------------
return scene
You should declare newBalloon as local when you initialize it in createBalloon(). Also, mainGroup == nil when you pass it as the parent group to display.newImageRect(). For these reasons, newBalloon is probably nil when you pass it to physics.addBody() instead of being a DisplayObject, which is what that method requires as its first argument.
For safety, in createBalloon(), you could do this:
local newBalloon = display.newImageRect( ... )
if newBalloon then
physics.addBody( newBalloon, ... )
else
print("WARNING: newBalloon is nil in createBalloon")
end
By the way, if you want all the balloons to be in the same GroupObject, you could add this:
local balloonGroup -- forward declaration
...
local function createBalloon()
local newBalloon = display.newImageRect( balloonGroup, ... )
...
end
local scene:create()
...
balloonGroup = display.newGroup()
sceneGroup:insert( balloonGroup )
balloonGroup:toFront()
...
balloonGroup:insert( balloon )
balloonGroup:insert( balloon2 )
...
end

My buttons wont appear in corona sdk

Can anyone help me. i am facing the below issue:
I am developing a game that is about questions and answers.
i used widget API to add buttons.
in menu screen (Menu.lua) i added 2 buttons for now. Play and options.
once i press play it go to the 1st level.
now the problem is as below:
1- once i add the buttons to the groupScene. they disappear.
2- once i remove them from groupScene they will appear. and will appear in the next seen.
so what should i do. should i remove them one by one. or is there another solution.
code as below
1- Main.Lua
`W = display.viewableContentWidth
H = display.viewableContentHeight
local widget = require( "widget")
local composer = require("composer" )
composer.purgeOnSceneChange = true
display.setStatusBar(display.HiddenStatusBar)
-- most commonly used screen coordinates
centerX = display.contentCenterX
centerY = display.contentCenterY
screenLeft = display.screenOriginX
screenWidth = display.contentWidth - screenLeft * 2
screenRight = screenLeft + screenWidth
screenTop = display.screenOriginY
screenHeight = display.contentHeight - screenTop * 2
screenBottom = screenTop + screenHeight
display.contentWidth = screenWidth
display.contentHeight = screenHeight
local bg = display.newRect( 0, 0, 2000,3000 )
bg:setFillColor( .26, .26, .26 )
composer.gotoScene( "Menu",{effect = "slideDown"} )
`
2- Menu.lua
local sceneName = ...
local composer = require( "composer" )
local scene = composer.newScene("Menu")
-- -----------------------------------------------------------------------------------------------------------------
-- All code outside of the listener functions will only be executed ONCE unless "composer.removeScene()" is called
-- -----------------------------------------------------------------------------------------------------------------
-- Local forward references should go here
-- -------------------------------------------------------------------------------
local function tapped(event)
local id = event.target.id
if id == "Start" then
composer.gotoScene( "Level1", {effect = "slideDown"} )
print("Level 1")
else
composer.gotoScene( "About_Us", {effect = "slideDown"} )
print("about us")
end
end
-- "scene:create()"
function scene:create( event )
local widget = require( "widget")
local sceneGroup = self.view
-- Initialize the scene here
-- Example: add display objects to "sceneGroup", add touch listeners, etc.
--local title = display.newText( "abed" , centerX, centerY ,"Helvetica", 20 )
------------------------------------------start
startButton = widget.newButton {
id = "Start",
onRelease = tapped,
defaultFile = "button.png",
overFile = "buttonclicked.png",
label = "إبدأ اللعبة",
labelColor = { default={ 1, 1, 1 } },
}
startButton.x=centerX
startButton.y = centerY+-50
--sceneGroup:insert( startButton )
------------------------------------------options
optionbutton = widget.newButton {
id = "options",
onRelease = tapped,
defaultFile = "button.png",
overFile = "buttonclicked.png",
label = "خيارات",
labelColor = { default={ 1, 1, 1 } }
}
optionbutton.x=centerX
optionbutton.y = centerY+50
--sceneGroup:insert( optionbutton )
--local start = display.newImageRect( [parent,], filename, [baseDir,], width, height )
end
-- "scene:show()"
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Called when the scene is still off screen (but is about to come on screen)
elseif ( phase == "did" ) then
-------------------------------------------code
end
end
-- "scene:hide()"
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Called when the scene is on screen (but is about to go off screen)
-- Insert code here to "pause" the scene
-- Example: stop timers, stop animation, stop audio, etc.
elseif ( phase == "did" ) then
-- Called immediately after scene goes off screen
end
end
-- "scene:destroy()"
function scene:destroy( event )
local sceneGroup = self.view
-- Called prior to the removal of scene's view
-- Insert code here to clean up the scene
-- Example: remove display objects, save state, etc.
end
-- -------------------------------------------------------------------------------
-- Listener setup
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- -------------------------------------------------------------------------------
return scene
3- Level1.lua
local sceneName = ...
local composer = require( "composer" )
local scene = composer.newScene("Level1")
-- -----------------------------------------------------------------------------------------------------------------
-- All code outside of the listener functions will only be executed ONCE unless "composer.removeScene()" is called
-- -----------------------------------------------------------------------------------------------------------------
local function tapped(event)
local id = event.target.id
if id == "option1" then
local answerCover = display.newRect( centerX, centerY, 400, 35)
answerCover:setFillColor( .26, .26, .26 )
local title = display.newText( "لقد ربحت" , centerX, centerY ,"Helvetica", 20 )
composer.gotoScene( "About_Us")
end
if id == "Option2" then
local answerCover = display.newRect( centerX, centerY, 400, 35)
answerCover:setFillColor( .26, .26, .26 )
local title = display.newText( "لقد خسرت" , centerX, centerY ,"Helvetica", 20 )
end
if id == "Option3" then
local answerCover = display.newRect( centerX, centerY, 400, 35)
answerCover:setFillColor( .26, .26, .26 )
local title = display.newText( "لقد خسرت" , centerX, centerY ,"Helvetica", 20 )
end
if id == "option4" then
local answerCover = display.newRect( centerX, centerY, 400, 35)
answerCover:setFillColor( .26, .26, .26 )
local title = display.newText( "لقد خسرت" , centerX, centerY ,"Helvetica", 20 )
end
end
-- Local forward references should go here
-- -------------------------------------------------------------------------------
-- "scene:create()"
function scene:create( event )
local widget = require( "widget")
local sceneGroup = self.view
local Q = display.newImageRect( "Questions/Q1.png", 300, 150 )
Q.x = centerX
Q.y = centerY-150
------------------------------------------option1
local option1 = widget.newButton {
id = "option1",
onRelease = tapped,
defaultFile = "button.png",
overFile = "buttonclicked.png",
label = "12",
labelColor = { default={ 1, 1, 1 } }
}
option1.x=centerX
option1.y = screenBottom-180
--sceneGroup:insert(option1)
------------------------------------------option2
local option2 = widget.newButton {
id = "Option2",
onRelease = tapped,
defaultFile = "button.png",
overFile = "buttonclicked.png",
label = "5",
labelColor = { default={ 1, 1, 1 } }
}
option2.x=centerX
option2.y = screenBottom - 130
------------------------------------------option3
local option3 = widget.newButton {
id = "Option3",
onRelease = tapped,
defaultFile = "button.png",
overFile = "buttonclicked.png",
label = "9",
labelColor = { default={ 1, 1, 1 } }
}
option3.x=centerX
option3.y = screenBottom-80
------------------------------------------option4
local option4 = widget.newButton {
id = "option4",
onRelease = tapped,
defaultFile = "button.png",
overFile = "buttonclicked.png",
label = "4",
labelColor = { default={ 1, 1, 1 } }
}
option4.x=centerX
option4.y = screenBottom -30
-- Initialize the scene here
-- Example: add display objects to "sceneGroup", add touch listeners, etc.
-- local title = display.newText( "abed" , centerX, centerY ,"Helvetica", 20 )
end
-- "scene:show()"
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Called when the scene is still off screen (but is about to come on screen)
elseif ( phase == "did" ) then
-- Called when the scene is now on screen
-- Insert code here to make the scene come alive
-- Example: start timers, begin animation, play audio, etc.
end
end
-- "scene:hide()"
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Called when the scene is on screen (but is about to go off screen)
-- Insert code here to "pause" the scene
-- Example: stop timers, stop animation, stop audio, etc.
elseif ( phase == "did" ) then
-- Called immediately after scene goes off screen
end
end
-- "scene:destroy()"
function scene:destroy( event )
local sceneGroup = self.view
-- Called prior to the removal of scene's view
-- Insert code here to clean up the scene
-- Example: remove display objects, save state, etc.
end
-- -------------------------------------------------------------------------------
-- Listener setup
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- -------------------------------------------------------------------------------
return scene

Lua - how do storyboard scenes transition to composer scenes?

My storyboard scenes transition to each other and so do my composer scenes. But when I want a storyboard scene to transition to a composer scene the screen goes dark and things stop dead.
Scene one - storyboard:
----------------------------------------
-- firstBar1.lua
----------------------------------------
local storyboard = require "storyboard"
local scene = storyboard.newScene()
function scene:createScene(event)
local screenGroup = self.view
local widget = require "widget"
local function onFirstBar2BtnRelease()
storyboard.gotoScene("firstBar2", "fade", 40)
return true
end
image1 = "images/staveBlankgrey2.png" -- the only button necessary
local firstBar2Btn = widget.newButton{
defaultFile = image1,
width = 480, height = 320,
onRelease = onFirstBar2BtnRelease
}
screenGroup:insert(firstBar2Btn)
-- clef24
image2 = display.newImageRect("images/clef24C.png", 100, 118)
image2.x = display.contentWidth
image2.y = display.contentHeight
image2.x, image2.y = 62, 149
screenGroup:insert(image2)
-- workout count
image3 = display.newImageRect("images/ex1of4.png", 60, 30)
image3.x = display.contentWidth
image3.y = display.contentHeight
image3.x, image3.y = 32, 20
screenGroup:insert(image3)
-- note1
image4 = display.newImageRect("images/crUp.png", 40, 75)
image4.x = display.contentWidth
image4.y = display.contentHeight
image4.x, image4.y = 170, 167
screenGroup:insert(image4)
-- note2
image5 = display.newImageRect("images/crDown.png", 40, 75)
image5.x = display.contentWidth
image5.y = display.contentHeight
image5.x, image5.y = 320, 142
screenGroup:insert(image5)
end
scene:addEventListener("createScene", scene)
return scene
Second scene - composer:
-----------------------------------------------
-- firstBar2.lua
-----------------------------------------------
local composer = require ( "composer" )
local scene = composer.newScene()
local function showEvenBars()
local options = {
effect = "slideLeft",
time = 30,
}
composer.gotoScene( "evenBars", options )
end
-- create scene
function scene:createScene ( event )
local sceneGroup = self.view
end
The code works up to here.
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will") then
local background = display.newImage("images/staveBlankgrey2.png", 240, 160)
note1 = display.newImage( "images/crUp.png", 130, 187)
note2 = display.newImage( "images/crUp.png", 320, 187)
sceneGroup:insert( background )
sceneGroup:insert( note1 )
sceneGroup:insert( note2 )
elseif ( phase == "did") then
timer.performWithDelay(tempo, showEvenBars)
end
end
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
end
function scene:destroy( event )
local sceneGroup = self.view
end
scene:addEventListener( "create", scene)
scene:addEventListener( "show", scene)
scene:addEventListener( "hide", scene)
scene:addEventListener( "destroy", scene)
return scene
This second scene, firstBar2 lua doesn't come on screen.
I'd be very grateful for any suggestions.
It is impossible to switch between storyboard and composer scene because they two are different libraries so their structure is different.
If you want your app to run properly you must always use one scene manager.
I recommend Composer.

dispatchEvent error in storyboard

When i pressed the credits button from the menu it gave me an error "?:0: attempt to call method 'dispatchEvent' (a nil value) ?:in function 'gotoScene'...scene_menu.lua:35: in function ...scene_menu.lua:33>?:in function "...Whats the problem here?
This is my menu page
-- scene_menu.lua
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
-- Clear previous scene
storyboard.removeAll()
--forward references
local background
local title
local playGame
local tutorial
local credits
display.setStatusBar(display.HiddenStatusBar)
function playgameBtn( event )
transition.to(playGame, {time = 1000, alpha = 0, xScale = 2, yScale = .6})
storyboard.gotoScene("playgame", "fade", 1000)
end
function tutorialBtn (event)
transition.to(tutorial, {time = 1000, alpha = 0, xScale = 2, yScale = .6})
storyboard.gotoScene("tutorial", "fade", 1000)
end
function creditsBtn (event)
transition.to(credits, {time = 1000, alpha = 0, xScale = 2, yScale = .6})
storyboard.gotoScene("credits", "fade", 1000)
end
function scene:createScene(event)
local group = self.view
background = display.newImage( "bkg_clouds.png")
group:insert(background)
background.x = 240
background.y = 195
title = display.newText("Shoot the Balloons!", 250, 100, native.systemFont, 25 )
title:setFillColor( 1,1, 0 )
playgame = display.newImage("Play Button.png")
group:insert(playgame)
playgame.x = 250
playgame.y = 150
tutorial = display.newImage("Tutorial Button.png")
group:insert(tutorial)
tutorial.x = 250
tutorial.y = 200
credits = display.newImage("Credits Button.png")
group:insert(credits)
credits.x = 250
credits.y = 250
end
function scene:enterScene( event )
playgame:addEventListener("tap", playgameBtn)
tutorial:addEventListener("tap", tutorialBtn)
credits:addEventListener("tap", creditsBtn)
end
function scene:exitScene(event)
playgame:removeEventListener("tap", playgameBtn)
tutorial:removeEventListener("tap", tutorialBtn)
credits:removeEventListener("tap", creditsBtn)
end
function scene:destroyScene(event)
end
scene:addEventListener("createScene", scene)
scene:addEventListener("enterScene", scene)
scene:addEventListener("exitScene", scene)
scene:addEventListener("destroyScene", scene)
return scene
This is my main.lua
-- main.lua
display.setStatusBar( display.HiddenStatusBar )
local storyboard = require "storyboard"
storyboard.gotoScene( "scene_splash" )
change the name credits.lua to the other name or change the variable name credits global variable,i think its due to the conflict of name in this line:storyboard.gotoScene("credits", "fade", 1000)

Resources