back to previous scene (with storyboard)

Hi!

How I can return to a previous scene using “Storyboard”?
I have two scenes, Scene 1 and Scene 2, where step 1 to 2 no problem. But when I come back from 2 to 1 I get this error:

: 0: attempt to concatenate global ‘sceneName’ (a nil value)
stack traceback:
[C]:?
: In function ‘gotoScene’

Thanks! [import]uid: 98258 topic_id: 34428 reply_id: 334428[/import]

Can you post your code where you are trying to return to scene 1?
[import]uid: 199310 topic_id: 34428 reply_id: 136840[/import]

in MAIN.LUA:
[lua]display.setStatusBar( display.HiddenStatusBar )

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

local function comenzar ()

storyboard.gotoScene( “scene1”, “slideLeft”, 800 )

end
playbutton = display.newImage(“playbutton.png”)
playbutton.x = 190
playbutton.y = 400
playbutton:addEventListener( “touch”, comenzar )

return scene[/lua]
in SCENE1.lua

[lua]module(…, package.seeall)

local storyboard = require( “storyboard” )
local scene2 = storyboard.newScene()

local function comenzar ()

storyboard.gotoScene( “main”, “slideLeft”, 800 )

end
playbutton = display.newImage(“playbutton2.png”)
playbutton.x = 190
playbutton.y = 400
playbutton:addEventListener( “touch”, back )

return scene2[/lua] [import]uid: 98258 topic_id: 34428 reply_id: 136885[/import]

The first thing I notice in your code is that you are calling the function “back” from your event listener but you have no “back” function to handle the touch event. It seems you carried over the function named “comenzar”.

If you rename your touch handle function in SCENE1 to “back” does it solve the issue? [import]uid: 190375 topic_id: 34428 reply_id: 136906[/import]

change some of the code to show the essential and had a typographical error, the error still comes from another place that do not know what can be. No is back, is comenzar. [import]uid: 98258 topic_id: 34428 reply_id: 136909[/import]

Well the error “attempt to concatenate global ‘sceneName’ (a nil value)” usually comes from trying to print a nil value like print("my scene name = " … sceneName) and sceneName is empty or you are trying to combine two values in your code somewhere else.

The bottom line is that you need to search your code for sceneName and see where you are trying to access it or print it and make sure the value is not nil.

if (not sceneName) then
print(“sceneName is empty”)
else
print("my scene name = " … sceneName)
end [import]uid: 190375 topic_id: 34428 reply_id: 136910[/import]

What is sceneName? I put it as an empty variable? sorry but I do not understand :frowning: [import]uid: 98258 topic_id: 34428 reply_id: 136912[/import]

(I’m wrong topic, I deleted this comment because it goes here) [import]uid: 98258 topic_id: 34428 reply_id: 136913[/import]

This error is telling you that sceneName is empty and you can’t do something with this value when it’s emtpy. You need to look in your code where sceneName is being used and find out why it’s empty.

Do you have a variable in your code named “sceneName” ?
[import]uid: 190375 topic_id: 34428 reply_id: 136916[/import]

I not :s [import]uid: 98258 topic_id: 34428 reply_id: 136917[/import]

resembles what it is: local scene = storyboard.newScene()
[import]uid: 98258 topic_id: 34428 reply_id: 136918[/import]

Without you complete code it will be hard to help you any further. [import]uid: 190375 topic_id: 34428 reply_id: 136919[/import]

usually need something else besides the gotoscene?
When I can not put the main.lua module (…, package.seall) can influence this?
Should I delete the scene before moving to another?
Actually the code is that, the rest are other things like game logic and some other random, no more.
I follow his reply.
Thanks for everything!
[import]uid: 98258 topic_id: 34428 reply_id: 136921[/import]

If it helps, the error I get when I return to a scene we’ve ever been. If a 2 Scene1 Principality of no error, if I spend 2-1 Scene no error, but if you step 1-2 and 2-1 I get the error that comment and no change of scene [import]uid: 98258 topic_id: 34428 reply_id: 136922[/import]

If it helps, the error I get when I return to a scene we’ve ever been. If a 2 Scene1 Principality of no error, if I spend 2-1 Scene no error, but if you step 1-2 and 2-1 I get the error that comment and no change of scene [import]uid: 98258 topic_id: 34428 reply_id: 136923[/import]

At this point what I would do is create a new project and add your code in a little at a time so that you are able to get just basic scene back and forth working. Thus 1 to 2, then 2 to 1.

I would use the template shown here: http://www.coronalabs.com/blog/2011/11/14/introducing-the-storyboard-api/

Make sure to add your playbutton (any images) to your scene group so they are destroyed when you exit the scene. The link above explains all of this in much more depth.

Also, I think it’s best that your main.lua file does not do too much and that you don’t return to the main.lua file from scene2. Instead after setting up essentials in main.lua you then transition (go to scene) such as “menu.lua” or “scene1.lua” then from “scene2.lua” you would go back to menu.lua or “scene1.lua” for additional options. Don’t go back to main.lua.

[import]uid: 190375 topic_id: 34428 reply_id: 136927[/import]

I’ll try and tomorrow I mention (Today I’m already in New Year in Spain;)) Anyway without main.lua found in the menu of the game, if I can not I do a file back menu.lua? so forget the main to use only once?

Thank you very much for everything and happy new year! [import]uid: 98258 topic_id: 34428 reply_id: 136930[/import]

There are several things going on. First main.lua is never a scene that you can go back to. It’s a launching point. Most likely you want a flow like this:

main.lua -> menu.lua -> scene1.lua

Then scene1.lua can go back to menu.lua. I’m sure if you worked hard enough you could manage to get main.lua to have storyboard like qualities, but there is a very important programming principle. “Don’t fight the system”. Attempting to go back to main.lua is fighting the system.

Next your scenes are not standard storyboard scenes. There is a very specific template that you need to follow for each of your scenes and your scenes are not following that template. Your scene1 should be written like this:

local storyboard = require( "storyboard" )  
local scene = storyboard.newScene()  
   
local function comenzar ()   
 storyboard.gotoScene( "main", "slideLeft", 800 ) --end  
  
--  
-- put all scene related graphics and sound creation here if you want them created before the   
-- scene transitions on screen. Must be at least one thing created here.  
--  
   
function scene:createScene(event)  
 local group = self.view  
   
 playbutton = display.newImage("playbutton2.png")  
 playbutton.x = 190  
 playbutton.y = 400  
 playbutton:addEventListener( "touch", back )  
 group:insert(playButton) --  
end  
  
--  
-- Put things here that need created after the scene is on the screen, like timers, audio voice overs,  
-- runtime listeners, etc. Anything you create here like timers and runtime listeners must be removed  
-- in exitScene. For most scenes you don't need to do anything here.  
--  
function scene:enterScene( event )  
 local group = self.view  
end  
  
function scene:exitScene( event )  
 local group = self.view  
end  
  
function scene:destroyScene( event )  
 local group = self.view  
end  
  
scene:addEventListener( "createScene", scene )  
scene:addEventListener( "enterScene", scene )  
scene:addEventListener( "exitScene", scene )  
scene:addEventListener( "destroyScene", scene )  
  
return scene  

That is the minimum code for a storyboard scene and until you are comfortable with it, do not rename things like: scene to scene2 or group to localGroup, etc. You’re just making things hard on yourself if you do.

Your main.lua should be:

display.setStatusBar( display.HiddenStatusBar )   
local storyboard = require( "storyboard" )  
  
storyboard.gotoScene("scene1")  

or a menu scene if you set one up. Main is just a launching point.

Finally, you have a bug in your code (which I did not fix above). Your touch handler is looking for a function called “back”, but you named it “comenzar”. You will get an assertion error because storyboard can’t find the “back” function.
[import]uid: 199310 topic_id: 34428 reply_id: 136931[/import]

well! and a shift of scene and everything ok, but when I go back to the main scene of the game, the game does not run. For example, variables should be lives which defaults to 3 remain at 0. And a feature called logic () does not run when I return to the main game screen.
Any suggestions? [import]uid: 98258 topic_id: 34428 reply_id: 136993[/import]

I did a test from scratch as you told me, I show you the code and errors:

main.lua
[lua]display.setStatusBar( display.HiddenStatusBar )
local storyboard = require( “storyboard” )

storyboard.gotoScene(“scene1”)[/lua]

scene1.lua

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

local function back ()
storyboard.gotoScene( “scene2”, “slideLeft”, 800 ) --end


– put all scene related graphics and sound creation here if you want them created before the
– scene transitions on screen. Must be at least one thing created here.

function scene:createScene(event)
local group = self.view

playbutton = display.newImage(“ene5.png”)
playbutton.x = 190
playbutton.y = 200
playbutton:addEventListener( “touch”, back )
group:insert(playButton) –
end


– Put things here that need created after the scene is on the screen, like timers, audio voice overs,
– runtime listeners, etc. Anything you create here like timers and runtime listeners must be removed
– in exitScene. For most scenes you don’t need to do anything here.

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

function scene:exitScene( event )
local group = self.view
end

function scene:destroyScene( event )
local group = self.view
end

scene:addEventListener( “createScene”, scene )
scene:addEventListener( “enterScene”, scene )
scene:addEventListener( “exitScene”, scene )
scene:addEventListener( “destroyScene”, scene )

return scene[/lua]

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

local function back ()
storyboard.gotoScene( “scene1”, “slideLeft”, 800 ) --end


– put all scene related graphics and sound creation here if you want them created before the
– scene transitions on screen. Must be at least one thing created here.

function scene:createScene(event)
local group = self.view

playbutton = display.newImage(“ene5.png”)
playbutton.x = 190
playbutton.y = 400
playbutton:addEventListener( “touch”, back )
group:insert(playButton) –
end


– Put things here that need created after the scene is on the screen, like timers, audio voice overs,
– runtime listeners, etc. Anything you create here like timers and runtime listeners must be removed
– in exitScene. For most scenes you don’t need to do anything here.

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

function scene:exitScene( event )
local group = self.view
end

function scene:destroyScene( event )
local group = self.view
end

scene:addEventListener( “createScene”, scene )
scene:addEventListener( “enterScene”, scene )
scene:addEventListener( “exitScene”, scene )
scene:addEventListener( “destroyScene”, scene )

return scene[/lua]

ERROR:
Runtime error
/Users/user/Desktop/storys/scene2.lua:20: ERROR: table expected. If this is a function call, you might have used ‘.’ instead of ‘:’
stack traceback:
[C]: ?
[C]: in function ‘insert’
/Users/user/Desktop/storys/scene2.lua:20: in function
?: in function ‘dispatchEvent’
?: in function ‘gotoScene’
/Users/user/Desktop/storys/scene1.lua:5: in function
?: in function <?:229>

[import]uid: 98258 topic_id: 34428 reply_id: 136994[/import]