corona storyboard dispatch event error - coronasdk

I am getting the below error in corona simulator. Nothing has changed in my files that I know of. ( I have included the main.lua code and the menuMain.lua code.) If you have any ideas on what might be causing this would be appricated! =) thanks.
Corona Simulator Runtime Error
File: ?
Attempt to call method 'dispatchEvent' (a nil value)
stack traceback:
[c] in function 'dispatchEvent'
? in funciton gotoScene
...dwegrecki/main.lua:16 in main chunck
-- main.lua
-- Hide Status Bar
display.setStatusBar(display.HiddenStatusBar)
-- require controller module
local storyboard = require "storyboard"
-- load first screen
storyboard.gotoScene( "menuMain" )
-- menuMain.lua
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
---------------------------------------------------------------------------------
-- BEGINNING OF IMPLEMENTATION
---------------------------------------------------------------------------------
local mainBg
local titleIcon
local startBtn
local aboutBtn
local onStartBtnTouch = {}
local scene = {}
-- Touch event listener for startBtn
local function onStartBtnTouch( self, event )
if event.phase == "began" then
storyboard.gotoScene( "menuLevel", "fade", 400 )
return true
end
end
-- Touch event listener for cerditsBtn
local function onCreditsBtnTouch( self, event )
if event.phase == "began" then
storyboard.gotoScene( "zerocredits", "fade", 400 )
return true
end
end
-- Called when the scene's view does not exist:
function scene:createScene( event )
local screenGroup = self.view
mainBg = display.newImage('bg.png')
titleBg = display.newImage('title.png', 0, 100)
playBtn = display.newImage('playBtn.png', 200, 240)
creditsBtn = display.newImage('creditsBtn.png', 200, 290)
titleView = display.newGroup(titleBg, playBtn, creditsBtn)
screenGroup:insert( titleBg, playBtn, creditsBtn )
playBtn.touch = onStartBtnTouch
creditsBtn.touch = onCreditsBtnTouch
end
-- Called immediately after scene has moved onscreen:
function scene:enterScene( event )
print( "1: enterScene event" )
-- remove previous scene's view
storyboard.purgeScene( "LevelMenu" )
storyboard.purgeScene( "credits" )
storyboard.purgeScene("main")
end
-- Called when scene is about to move offscreen:
function scene:exitScene( event )
print( "1: exitScene event" )
-- remove touch listener for image
mainBg:removeEventListener( "touch", mainBg )
end
-- Called prior to the removal of scene's "view" (display group)
function scene:destroyScene( event )
print( "((destroying scene 1's view))" )
end
---------------------------------------------------------------------------------
-- END OF IMPLEMENTATION
---------------------------------------------------------------------------------
scene:addEventListener( "createScene", scene )
scene:addEventListener( "enterScene", scene )
scene:addEventListener( "exitScene", scene )
scene:addEventListener( "destroyScene", scene )
---------------------------------------------------------------------------------
return scene

i recreate same project like your's and i'm getting an error on local scene = {} why did you make the variable scene a table ?

Related

Corona SDK composer - changing scenes doesn't work

I have a little problem with corona SDK composer API. I got 3 .lua files- main.lua, character.lua and job.lua. The problem is, when I run the game, it automatically transits to character.lua and everything's fine. Then I'm transiting to job.lua and it works as well, but when I'm trying to come back to character.lua- nothing happens (the background doesn't change).That's how it look like:
main.lua:
local composer = require("composer")
composer.gotoScene("character")
character.lua:
local composer = require( "composer" )
local scene = composer.newScene()
-- -----------------------------------------------------------------------------------------------------------------
-- All code outside of the listener functions will only be executed ONCE unless "composer.removeScene()" is called.
-- -----------------------------------------------------------------------------------------------------------------
-- local forward references should go here
-- -------------------------------------------------------------------------------
-- "scene:create()"
function scene:create( event )
local sceneGroup = self.view
-- Initialize the scene here.
-- Example: add display objects to "sceneGroup", add touch listeners, etc.
local widget = require("widget")
local bottomTabButtons ={
{ width=32, height=32, defaultFile="character.png", overFile="character_active.png", selected="true"},
{ width=32, height=32, defaultFile="job.png", overFile="job_active.png", onPress=function() composer.gotoScene( "job" )end},
}
local bottomBar = widget.newTabBar{
top = display.contentHeight-40,
buttons = bottomTabButtons
}
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 ("sceneGroup").
-- 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
job.lua:
local composer = require( "composer" )
local widget = require("widget")
local scene = composer.newScene()
-- -----------------------------------------------------------------------------------------------------------------
-- All code outside of the listener functions will only be executed ONCE unless "composer.removeScene()" is called.
-- -----------------------------------------------------------------------------------------------------------------
-- local forward references should go here
-- -------------------------------------------------------------------------------
-- "scene:create()"
function scene:create( event )
local sceneGroup = self.view
-- Initialize the scene here.
-- Example: add display objects to "sceneGroup", add touch listeners, etc.
local background = display.newImage("background.png")
background.x=display.contentCenterX
background.y=display.contentCenterY
background.height=display.contentHeight
background.width=display.contentWidth
local function gotoCharacter(event)
composer.gotoScene("character")
print('asd')
end
local bottomTabButtons ={
{ width=32, height=32, defaultFile="character.png", overFile="character_active.png", onPress=function() composer.gotoScene( "character" ) end},
{ width=32, height=32, defaultFile="job.png", overFile="job_active.png", selected="true"},
}
local bottomBar = widget.newTabBar{
top = display.contentHeight-40,
buttons = bottomTabButtons
}
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 ("sceneGroup").
-- 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
Thanks in advance!
The problem is your not inserting your display objects in to the scenegroup. Thats why its not getting deleted. Insert all your objects in to the scenegroup.
For example,
local background = display.newImage("background.png")
background.x=display.contentCenterX
background.y=display.contentCenterY
background.height=display.contentHeight
background.width=display.contentWidth
sceneGroup:insert(background)
--- This is the change. Same way insert all the objects in to this group.

corona composer background next scene not showing

I have a lot of difficulties with composer...it's not so easy!
My previous scene is "game" (game.lua) everything is ok
When i go to my scene "recolt" with this snippet inside my game lua :
local function goTo()
print("gotoscene")
composer.gotoScene( "recolt", { time = 1000, effect = "fromRight", params = params } )
end
timer.performWithDelay( 1000, goTo )
:The problem is that i see my previous scene behind my recolt scene and i can't see my background but i see my circle who's moving ??? however my sceneGroup is correct :
sceneGroup:insert(background,circle)
When i do :
sceneGroup:insert(circle,background)
I see my background who hide my previous scene and my circle. > behavior expected
What's wrong? i would like to see my background and my circle in y "recolt" scene. Could you help me ? Thanks a lot...Below the snippet "recolt.lua".
--recolt.lua
local composer = require( "composer" )
local scene = composer.newScene()
function scene:create( event )
local sceneGroup = self.view
params = event.params
local background=display.newImageRect("back02.png",display.contentWidth*.5,d isplay.contentHeight*.5,320,480)
background.x,background.y=display.contentWidth*.5,display.contentHeight*.5
background.xScale,background.yScale=2,2
background.alpha=1
local circle = display.newCircle(120,100,100)
sceneGroup:insert(background,circle)
timeT=150
local function tr4()
transition.to(circle,{time=timeT,x=200,y=300,alpha=1,transition=easing.linear, xScale=1,yScale=1})
end
local function tr3()
transition.to(circle,{time=200,x=100,y=300,transition=easing.linear, xScale=.2 ,yScale=.5,onComplete=tr4})
end
local function tr2()
transition.to(circle,{time=200,x=200,y=295,transition=easing.linear, xScale=.2 ,yScale=.2,alpha=.2,onComplete=tr3})
end
local function tr1()
transition.to(circle,{time=timeT,x=300,y=300,transition=easing.linear, xScale= .5,yScale=.5,onComplete=tr2})
end
timer.performWithDelay(700,tr1,-1)
end
function scene:show( event )
local sceneGroup = self.view
params = event.params
if event.phase == "did" then
--physics.start()
end
end
function scene:hide( event )
local sceneGroup = self.view
if event.phase == "will" then
--
-- Remove enterFrame listeners here
--
--physics.stop()
end
end
function scene:destroy( event )
local sceneGroup = self.view
end
---------------------------------------------------------------------------------
-- END OF YOUR IMPLEMENTATION
---------------------------------------------------------------------------------
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
return scene
You need to check the docs: object:insert()
The problem is you can only add 1 object into the scene with insert() function, so you only need to change
sceneGroup:insert(background,circle)
to
sceneGroup:insert(background)
sceneGroup:insert(circle)
The order matters, the first object you create go back in the scene.

My buttons wont appear on the screen for some reason in Corona SDK

My buttons wont appear on the screen for some reason in Corona SDK here is my code what am I missing?
local composer = require( "composer" )
local scene = composer.newScene()
-- include Corona's "widget" library
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
-- forward declarations and other locals
local playBtn
-- 'onRelease' event listener for playBtn
local function onPlayBtnRelease()
-- go to levelSelect.lua scene
composer.gotoScene( "levelSelect", "fade", 500 )
return true -- indicates successful touch
end
-- Background
local sky = display.newImage ("startScreen/sky.png")
sky.x = display.contentWidth/2; sky.y = display.contentHeight/2;
-- Picture
local preston = display.newImage ("startScreen/PrestonArt.png")
preston:scale( 0.4, 0.4 )
preston.x = display.contentWidth/2; preston.y = display.contentHeight/2;
-- Labels
local learningLabel = display.newImage ("startScreen/Learning.png")
learningLabel:scale( 0.3, 0.3 )
learningLabel.x = 506; learningLabel.y = 170;
local centerLabel = display.newImage ("startScreen/Center.png")
centerLabel:scale( 0.3, 0.3 )
centerLabel.x = 506; centerLabel.y = 600;
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.
-- create a widget button (which will loads levelSelect.lua on release)
playBtn = widget.newButton{
defaultFile = "startScreen/Play.png", --the "default" image file
overFile = "startScreen/Play-Over.png", --the "over" image file
width=240, height=120,
onRelease = onPlayBtnRelease -- event listener function
}
playBtn.x = 300; playBtn.y = 695;
-- all display objects must be inserted into group
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
My guesses are
Comment says go to level1.lua but it tries to go to levelSelect
Don't use upper case in file names (in general)
try removing "fade", 500 to see it it works
in levelselect.lua you did not return scene or did not handle scene:create
More code and info please

attempt to index global 'group' (a nil value) - lua

I am new to lua and corona sdk, and i am using the storyboard api to create an app. i have a main.lua which just sends the user to menu.lua. menu.lua code looks like this:
local storyboard = require( "storyboard" )
local scene = storyboard.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
storyboard.gotoScene( "level1", "fade", 500 )
return true -- indicates successful touch
end
-- Called when the scene's view does not exist:
function scene:createScene( event )
local group = self.view
-- display a background image
local background = display.newImageRect( "background.png", display.contentWidth, display.contentHeight )
background:setReferencePoint( display.TopLeftReferencePoint )
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:setReferencePoint( display.CenterReferencePoint )
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} },
fontSize = "40",
defaultFile="button.png",
overFile="button-over.png",
width=250, height=80,
onRelease = onPlayBtnRelease -- event listener function
}
playBtn:setReferencePoint( display.CenterReferencePoint )
playBtn.x = display.contentWidth*0.5
playBtn.y = display.contentHeight - 125
-- all display objects must be inserted into group
group:insert( background )
group:insert( titleLogo )
group:insert( playBtn )
end
-- Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local group = self.view
-- INSERT code here (e.g. start timers, load audio, start listeners, etc.)
end
-- Called when scene is about to move offscreen:
function scene:exitScene( event )
local group = self.view
-- INSERT code here (e.g. stop timers, remove listenets, unload sounds, etc.)
end
-- If scene's view is removed, scene:destroyScene() will be called just prior to:
function scene:destroyScene( event )
local group = self.view
if playBtn then
playBtn:removeSelf() -- widgets must be manually removed
playBtn = nil
end
end
-- "createScene" event is dispatched if scene's view does not exist
scene:addEventListener( "createScene", scene )
-- "enterScene" event is dispatched whenever scene transition has finished
scene:addEventListener( "enterScene", scene )
-- "exitScene" event is dispatched whenever before next scene's transition begins
scene:addEventListener( "exitScene", scene )
-- "destroyScene" event is dispatched before view is unloaded, which can be
-- automatically unloaded in low memory situations, or explicitly via a call to
-- storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( "destroyScene", scene )
-----------------------------------------------------------------------------------------
return scene
and this is my level1.lua:
-----------------------------------------------------------------------------------------
--
-- level1.lua
--
-----------------------------------------------------------------------------------------
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
-- include Corona's "physics" library
local physics = require "physics"
physics.start(); physics.pause()
--------------------------------------------
-- forward declarations and other locals
local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth*0.5
-----------------------------------------------------------------------------------------
-- BEGINNING OF YOUR IMPLEMENTATION
--
-- NOTE: Code outside of listener functions (below) will only be executed once,
-- unless storyboard.removeScene() is called.
--
-----------------------------------------------------------------------------------------
-- Called when the scene's view does not exist:
function scene:createScene( event )
local group = self.view
-- create a grass object
local bar= display.newImage( "bar.png", 0, 398)
local function update( event )
local group = self.view
updateBackgrounds()
end
local background1 = display.newImage("images/bg.png")
background1.x = 240
background1.y = 160
local background2 = display.newImage("images/bg.png")
background2.x = 760
background2.y = 160
function updateBackgrounds()
local group = self.view
--near background movement
background1.y = background1.y - (3)
--if the sprite has moved off the screen move it back to the
--other side so it will move back on
if(background1.x < -239) then
background1.x = 760
end
background2.y = background2.y - (3)
if(background2.x < -239) then
background2.x = 760
end
end
--this is how we call the update function, make sure that this line comes after the
--actual function or it will not be able to find it
--timer.performWithDelay(how often it will run in milliseconds, function to call,
--how many times to call(-1 means forever))
timer.performWithDelay(1, update, -1)
end
-- all display objects must be inserted into group
group:insert( background1 )
group:insert( background2 )
group:insert( bar )
-- Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local group = self.view
physics.start()
end
-- Called when scene is about to move offscreen:
function scene:exitScene( event )
local group = self.view
physics.stop()
end
-- If scene's view is removed, scene:destroyScene() will be called just prior to:
function scene:destroyScene( event )
local group = self.view
package.loaded[physics] = nil
physics = nil
end
-----------------------------------------------------------------------------------------
-- END OF YOUR IMPLEMENTATION
-----------------------------------------------------------------------------------------
-- "createScene" event is dispatched if scene's view does not exist
scene:addEventListener( "createScene", scene )
-- "enterScene" event is dispatched whenever scene transition has finished
scene:addEventListener( "enterScene", scene )
-- "exitScene" event is dispatched whenever before next scene's transition begins
scene:addEventListener( "exitScene", scene )
-- "destroyScene" event is dispatched before view is unloaded, which can be
-- automatically unloaded in low memory situations, or explicitly via a call to
-- storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( "destroyScene", scene )
-----------------------------------------------------------------------------------------
return scene
And here is my error:
When you create the group variable in the updateBackgrounds() method, self.view is nil because self.view only exists in the methods createScene() and enterScene(). What I recommend to do is create a global variable on the beginning of the script called group and then on your createScene method just do group = self.view without the "local" identifier.
Something like this:
--begining of the script
local group
-- Called when the scene's view does not exist:
function scene:createScene( event )
group = self.view
end
this creates a variable called group that has the reference of the self.view variable through all the script.
Another good option is to create a new global group and add it to the self.view on the createScene() method, this keeps things clean.
local globalGroup
function scene:createScene(event)
local group = self.view
globalGroup = display.newGroup()
group:insert(globalGroup)
end
Hope this helps.
This might because functions are not ending properly. Check if you are ending each function in level1.lua.
You are calling a function with a timer, but that function doesn't have access to self.view. Use the "enterFrame" event on Runtime, this is called at every frame:
function scene:enterFrame(event)
local group = self.view
... stuff from update functions ...
end
Runtime.addEventListener("enterFrame", scene)
If you don't need every frame, but say every second frame, put a counter in scene:enterFrame. Or in scene:enterFrame compute how much time since last call to determine if update needed.
local group
Should become
group
so that it is indeed global. Adding the local makes the 'global' become a part of only the body, not the functions, vice versa. If you need Lua practice, Roblox is a good place to start. The provide libraries already in your scripts so that it is easier to code. Try it. ;)

storyboard - multiple scene enterScene/existScene occuring - what's wrong with this code?

Have spent several hours trying to work out a Storyboard issue here. I've pruned a sample project down that replicates it.
Issue:
After going from scene 1, to scene 2, then back to scene 1, and then toggling through scenes a few times I get the following:
When entering a scene the logging shows multiple enterScene/exitScene are being triggered
It seems as through perhaps multiple background touch listeners are being registered, however I'm trying to remove these in the exitScene functions
I'm on the trial version: 2011.704 (2011.12.8)
Output
<code>
-- Click on background to go back to screen_example1
storyboard.gotoScene( screen_example1)
screen_example1: enterScene
storyboard.gotoScene( screen_example1)
screen_example1: exitScene
screen_example1: enterScene
storyboard.gotoScene( screen_example1)
screen_example1: exitScene
screen_example1: enterScene
storyboard.gotoScene( screen_example1)
screen_example1: exitScene
screen_example1: enterScene
.
.
.
etc
</code>
Code: Three files: main and the two scene files:
main.lua
<code>
display.setStatusBar( display.HiddenStatusBar )
local storyboard = require "storyboard"
storyboard.gotoScene( "screen_example1" )
</code>
screen_example1.lua
<code>
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
local function onScreenTouch( event )
if event.phase == "began" then
print("")
storyboard.gotoScene( "scene_towerView")
end
end
function scene:createScene( event )
print ("screen_example1 - createScene")
local image = display.newImage( "bg.jpg" )
scene.view:insert( image )
end
function scene:enterScene( event )
print ("screen_example1: enterScene")
scene.view:addEventListener( "touch", onScreenTouch )
end
function scene:exitScene( event )
print ("screen_example1: exitScene")
scene.view:removeEventListener("touch", onScreenTouch)
end
-- Scene Listeners
scene:addEventListener( "createScene", scene )
scene:addEventListener( "enterScene", scene )
scene:addEventListener( "exitScene", scene )
scene:addEventListener( "destroyScene", scene )
return scene
</code>
scene_towerView.lua
<code>
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
local function onScreenTouch( event )
if event.phase == "began" then
print("")
print("storyboard.gotoScene( screen_example1)")
storyboard.gotoScene( "screen_example1")
end
end
-- Scene Handlers
function scene:createScene( event )
print ("TowerScene - createScene")
-- -- Background Image
local image = display.newImage( "bg2.jpg" )
scene.view:insert( image )
end
function scene:enterScene( event )
print ("TowerScene - enterScene")
scene.view:addEventListener( "touch", onScreenTouch )
end
-- Scene Listeners
scene:addEventListener( "createScene", scene )
scene:addEventListener( "enterScene", scene )
scene:addEventListener( "exitScene", scene )
scene:addEventListener( "destroyScene", scene )
return scene
</code>
Did you try adding a "return true" at the end of the touch event code? This stumped me for the longest time.
Different layers in Corona/Lua
k

Resources