Corona going to previous scene results in black screen - lua

I've got this problem that is delaying my game by a milestone, in scene1 I click the menu button that takes me to the menu then when the user wants to play again they click the play button and they should go to the previous scene however when it does its goes to a black screen. here is some code, this is the main menu button in scene 1:
function scene:enterScene(event)
local group = self.view
function menubutton:touch( event )
if event.phase == "began" then
storyboard.gotoScene( "menu", "slideRight", 750 )
audio.play(click)
display.getCurrentStage():setFocus( event.target )
event.target.isFocus = true
elseif event.target.isFocus then
if event.phase == "moved" then
print( "user has moved their finger off the button." )
elseif event.phase == "ended" then
print( "user has switched to the main menu" )
display.getCurrentStage():setFocus( nil )
event.target.isFocus = false
end
end
return true
end
here is the play button on the main menu:
function scene:enterScene(event)
local group = self.view
local function onSceneTouch( event )
if event.phase == "ended" then
audio.play(click)
local previousScene = storyboard.getPrevious()
if previousScene == nil then
storyboard.gotoScene( "scene1", "slideLeft", 750 ) else
storyboard.gotoScene(previousScene)
return true
end
end
end
any ideas? I am getting NO errors in the simulator output.
Edit: This line of code stopped the blank screen when i placed it on the menu, but only the images show up, background image button images etc but nothing else.
local prior_scene = storyboard.getPrevious()
storyboard.purgeScene( prior_scene )

Try to use a tap listener instead of touch listener. I don't see your entire code but I think there is the problem.
main menu button in scene 1:
function scene:enterScene(event)
local group = self.view
local function onMenuButtonTap( event )
audio.play(click)
storyboard.gotoScene( "menu", "slideRight", 750 )
return true
end
end
play button on the main menu scene:
function scene:enterScene(event)
local group = self.view
local function onPlayTap( event )
audio.play(click)
local previousScene = storyboard.getPrevious()
if previousScene == nil then
storyboard.gotoScene( "scene1", "slideLeft", 750 )
else
storyboard.gotoScene(previousScene)
end
return true
end
end
------------------------------------------------------------
New code:
------------------------------------------------------------
change this in your scene1.lua
function scene:exitScene(event)
local group = self.view
storyboard.destroyScene( "scene1" )
end
with this
function scene:exitScene(event)
local group = self.view
end
In your menu.lua add this:
function scene:createScene( event )
local group = self.view
storyboard.purgeScene( "scene1" )
end
scene:addEventListener( "createScene", scene )

Related

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

In Corona SDK, how can I put a restart function at the end of my game?

I have already made a restart button, but I don't know how to attach a restart function to the button. If anyone could help, that would be great.
local widget = require("widget")
local function Restart(event)
if "began" == event.phase then
--code here when touch begin
elseif "moved" == event.phase then
--code here when move
elseif "ended" == event.phase or "cancelled" == event.phase then
--code here when touch end
end
end
-- Create the widget
local button1 = widget.newButton{
left = 100,
top = 200,
id = "button1",
label = "Label",
onEvent = Restart
}
http://docs.coronalabs.com/api/library/widget/newButton.html
You can have a look at it.
By the way, when restart your game don't forget init Variables & display.* (like display.newGroup() / display.newImage() etc.)

corona storyboard dispatch event error

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 ?

Clean data form scene

How can i clear all objects out of 'game.lua, when i leave the game scene all the data is still on the screen, how can i remove everything when i leave and reset it back to the start position when i go back to 'game.lua' ?
game.lua:
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
_W = display.contentWidth
_H = display.contentHeight
system.setIdleTimer(false); -- Prevent the app from becoming suspended
local physics = require "physics"
physics.start()
clouts = true
score = 0
speeda1 = 100
speedb1 = 150
function scene:createScene( event )
local group = self.view
end
function scene:enterScene( event )
local group = self.view
--start drop zone
if clouts then
local badclout1 = {}
local bad1Group = display.newGroup()
local function spawnBC1()
local badclouts1 = display.newImage("BCloud1.png")
badclouts1.x = math.random(0, _W)
physics.addBody( badclouts1, "dynamic", { density=.1, bounce=.1, friction=.2, radius=45 } )
badclouts1.name = "BCloud1"
badclouts1.bodyType = "kinematic"
badclouts1.isSensor = true
badclouts1.y = math.random(-100, -50)
badclouts1.index = #badclout1 + 1
bad1Group:insert(badclouts1)
badclouts1.rotation = math.random(-10,10) -- Rotate the object
badclouts1:setLinearVelocity(0, math.random(speeda1, speedb1)) -- Drop down
badclout1[badclouts1.index] = badclouts1
tmrSpawn1 = timer.performWithDelay(math.random(spawna, spawnb), spawnBC1)
return badclouts1
end
tmrSpawn1 = timer.performWithDelay(math.random(1000, 10000), spawnBC1)
local function removeBomb()
for i, v in pairs(badclout1) do
if badclout1[i].y >1000 then
badclout1[i]:removeSelf()
badclout1[i] = nil
end
end
end
Runtime:addEventListener("enterFrame", removeBomb)
end
-- end drop zone
local function speatTimer()
speeda1 = speeda1+1
speedb1 = speedb1+1
end
local mainTimer = timer.performWithDelay( 550, speatTimer, 200 )
function gameOver()
storyboard.gotoScene("restart", "fade", 400)
end
end
function scene:exitScene( event )
local group = self.view
Runtime:removeEventListener( "collision", onCollision )
Runtime:removeEventListener("accelerometer", onTilt)
Runtime:removeEventListener("enterFrame", removeBomb)
end
function scene:destroyScene( event )
local group = self.view
end
function scene:overlayEnded( event )
local group = self.view
end
scene:addEventListener( "createScene", scene )
scene:addEventListener( "enterScene", scene )
scene:addEventListener( "exitScene", scene )
scene:addEventListener( "destroyScene", scene )
scene:addEventListener( "overlayEnded", scene )
return scene
Regards Kevin,
if you are using storyboard and want to reset all the values you can remove the scene before you go to another scene so when you go again to the same scene all will be created
function scene:exitScene( event )
local group = self.view
Runtime:removeEventListener( "collision", onCollision )
Runtime:removeEventListener("accelerometer", onTilt)
Runtime:removeEventListener("enterFrame", removeBomb)
storyboard.removeScene("SCENE TO REMOVE")
end
or do this after you go to the scene
function scene:enterScene( event )
storyboard.removeScene("SCENE TO REMOVE")
end
here's the link on how to implement it.
http://www.coronalabs.com/blog/2012/07/31/storyboard-scene-purging-vs-removal/
There are two way to reset back your object positions and variable data
The first one is to create a function like this
function resetGame()
--your initial position and data values here
end
And call it whenever you need to reset your game but you will manually code the reset values.
The second one is by creating a dummy scene. A dummy scene is a scene that will redirect you again to the game scene like this
Dummy Scene
function scene:createScene( event )
local group = self.view
storyboard.gotoScene( "scenes.Game" ) --scene/Game.lua
end
to reset the values of your objects and variables, but don't forget to remove all of the listeners and put it on the exitScene() function when you go to the dummy scene.
Since you are using storyboard, why not simply insert all your display objects into group:
group:insert(someobject)
and let the scene manager do the work for you. By not creating the scene in scene:createScene() your ability to transition the screen won't work. By not putting things into the group, it can't remove them for you when the scene goes away.

Button gotoScene not working

the storyboard.gotoScene("facebook", "fade", 400) is not working if i tap the button, and i dont get any error messages in the terminal. What am i doing wrong ?
-- requires
display.setStatusBar( display.HiddenStatusBar )
_W = display.contentWidth; --Returns Screen Width
_H = display.contentHeight; --Returns Screen Height
local storyboard = require ("storyboard")
local scene = storyboard.newScene()
-- background
function scene:createScene(event)
local screenGroup = self.view
background = display.newImage("restart.png")
screenGroup:insert(background)
button = display.newImage("share2.png")
button.x = display.contentWidth / 2
button.y = display.contentHeight -400
end
function listener(event)
if event.phase == "began" then
print(event.name.." occurred")
storyboard.gotoScene("facebook", "fade", 400)
end
end
function scene:enterScene(event)
storyboard.purgeScene("game")
button:addEventListener( "tap", listener )
end
function scene:exitScene(event)
button:removeEventListener( "tap", listener )
end
function scene:destroyScene(event)
end
scene:addEventListener("createScene", scene)
scene:addEventListener("enterScene", scene)
scene:addEventListener("exitScene", scene)
scene:addEventListener("destroyScene", scene)
return scene
The "tap" event and "touch" events are different and they get different "phases" passed to the event handler. The way you have your event handler programmed, you're expecting "touch" events (began, ended, moved). The tap event doesn't really generate any phases, either you were tapped or not.
Either change these two lines:
button:addEventListener( "tap", listener )
button:removeEventListener( "tap", listener )
to:
button:addEventListener( "touch", listener )
and
button:removeEventListener( "touch", listener )
or you can change your listner to:
function listener(event)
print(event.name.." occurred")
storyboard.gotoScene("facebook", "fade", 400)
end
Try this:
storyboard.gotoScene("facebook", {effect = "fade", time=400})
Or:
local options =
{
effect = "fade",
time = 400,
}
storyboard.gotoScene("facebook", options)
http://docs.coronalabs.com/api/library/storyboard/gotoScene.html
It looks like corona does not like it when you call a Scene 'facebook' or renamed facebook to 'postmyscore' and it works
I had the same problem since Coronas last update. I fixed the problem by removing the if check for the event phase. Just comment out the event.phase check in your listener function:
function listener(event)
--if event.phase == "began" then
print(event.name.." occurred")
storyboard.gotoScene("facebook", "fade", 400)
end
facebook.lua is already integrated in the inbuild API , so it may generate the problem , more over you are using tap event , so in listener no need to check the phase or you may use touch event.

Resources