Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

back to previous scene (with storyboard)
Started by tore24 Dec 30 2012 03:55 AM

- - - - -
39 replies to this topic
[TOPIC CONTROLS]
Page 1 of 2 1 2
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

tore24

[GLOBAL: userInfoPane.html]
tore24
  • Contributor

  • 103 posts
  • Corona SDK

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!
uid: 98258 topic_id: 34428 reply_id: 334428


[TOPIC: post.html]
#2

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,605 posts
  • Enterprise

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


[TOPIC: post.html]
#3

tore24

[GLOBAL: userInfoPane.html]
tore24
  • Contributor

  • 103 posts
  • Corona SDK

in MAIN.LUA:
display.setStatusBar( display.HiddenStatusBar ) local storyboard = require( "storyboard" )local scene = storyboard.newScene()local function comenzar ()    storyboard.gotoScene( "scene1", "slideLeft", 800 )endplaybutton = display.newImage("playbutton.png")playbutton.x = 190playbutton.y = 400playbutton:addEventListener( "touch", comenzar )return scene

in SCENE1.lua

module(..., package.seeall)local storyboard = require( "storyboard" )local scene2 = storyboard.newScene()local function comenzar ()    storyboard.gotoScene( "main", "slideLeft", 800 )endplaybutton = display.newImage("playbutton2.png")playbutton.x = 190playbutton.y = 400playbutton:addEventListener( "touch", back )return scene2
uid: 98258 topic_id: 34428 reply_id: 136885


[TOPIC: post.html]
#4

Tim @ Playful Media

[GLOBAL: userInfoPane.html]
Tim @ Playful Media
  • Observer

  • 21 posts
  • Corona SDK

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?
uid: 190375 topic_id: 34428 reply_id: 136906


[TOPIC: post.html]
#5

tore24

[GLOBAL: userInfoPane.html]
tore24
  • Contributor

  • 103 posts
  • Corona SDK

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.
uid: 98258 topic_id: 34428 reply_id: 136909


[TOPIC: post.html]
#6

Tim @ Playful Media

[GLOBAL: userInfoPane.html]
Tim @ Playful Media
  • Observer

  • 21 posts
  • Corona SDK

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
uid: 190375 topic_id: 34428 reply_id: 136910


[TOPIC: post.html]
#7

tore24

[GLOBAL: userInfoPane.html]
tore24
  • Contributor

  • 103 posts
  • Corona SDK

What is sceneName? I put it as an empty variable? sorry but I do not understand :(
uid: 98258 topic_id: 34428 reply_id: 136912


[TOPIC: post.html]
#8

tore24

[GLOBAL: userInfoPane.html]
tore24
  • Contributor

  • 103 posts
  • Corona SDK

(I'm wrong topic, I deleted this comment because it goes here)
uid: 98258 topic_id: 34428 reply_id: 136913


[TOPIC: post.html]
#9

Tim @ Playful Media

[GLOBAL: userInfoPane.html]
Tim @ Playful Media
  • Observer

  • 21 posts
  • Corona SDK

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" ?
uid: 190375 topic_id: 34428 reply_id: 136916


[TOPIC: post.html]
#10

tore24

[GLOBAL: userInfoPane.html]
tore24
  • Contributor

  • 103 posts
  • Corona SDK

I not :s
uid: 98258 topic_id: 34428 reply_id: 136917


[TOPIC: post.html]
#11

tore24

[GLOBAL: userInfoPane.html]
tore24
  • Contributor

  • 103 posts
  • Corona SDK

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


[TOPIC: post.html]
#12

Tim @ Playful Media

[GLOBAL: userInfoPane.html]
Tim @ Playful Media
  • Observer

  • 21 posts
  • Corona SDK

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


[TOPIC: post.html]
#13

tore24

[GLOBAL: userInfoPane.html]
tore24
  • Contributor

  • 103 posts
  • Corona SDK

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!
uid: 98258 topic_id: 34428 reply_id: 136921


[TOPIC: post.html]
#14

tore24

[GLOBAL: userInfoPane.html]
tore24
  • Contributor

  • 103 posts
  • Corona SDK

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
uid: 98258 topic_id: 34428 reply_id: 136922


[TOPIC: post.html]
#15

tore24

[GLOBAL: userInfoPane.html]
tore24
  • Contributor

  • 103 posts
  • Corona SDK

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
uid: 98258 topic_id: 34428 reply_id: 136923


[TOPIC: post.html]
#16

Tim @ Playful Media

[GLOBAL: userInfoPane.html]
Tim @ Playful Media
  • Observer

  • 21 posts
  • Corona SDK

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.

uid: 190375 topic_id: 34428 reply_id: 136927


[TOPIC: post.html]
#17

tore24

[GLOBAL: userInfoPane.html]
tore24
  • Contributor

  • 103 posts
  • Corona SDK

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!
uid: 98258 topic_id: 34428 reply_id: 136930


[TOPIC: post.html]
#18

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,605 posts
  • Enterprise

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 )  --<--- you have to go somewhere besides main since it's not a storyboard scene.
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)  --<------ must do this for storyboard to be able manage the assets

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.
uid: 199310 topic_id: 34428 reply_id: 136931


[TOPIC: post.html]
#19

tore24

[GLOBAL: userInfoPane.html]
tore24
  • Contributor

  • 103 posts
  • Corona SDK

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?
uid: 98258 topic_id: 34428 reply_id: 136993


[TOPIC: post.html]
#20

tore24

[GLOBAL: userInfoPane.html]
tore24
  • Contributor

  • 103 posts
  • Corona SDK

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

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


scene1.lua

local storyboard = require( "storyboard" )local scene = storyboard.newScene() local function back ()     storyboard.gotoScene( "scene2", "slideLeft", 800 )  --


scene2.lua
local storyboard = require( "storyboard" )local scene = storyboard.newScene() local function back ()     storyboard.gotoScene( "scene1", "slideLeft", 800 )  --


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

uid: 98258 topic_id: 34428 reply_id: 136994


[TOPIC: post.html]
#21

Tim @ Playful Media

[GLOBAL: userInfoPane.html]
Tim @ Playful Media
  • Observer

  • 21 posts
  • Corona SDK

Ok, this error is because you have a spelling (case) issue.

In both scenes you have defined your image as "playbutton" but in the group:insert method you are spelling it "playButton". The "B" is what is causing your errors. Lua is case sensitive.

You should also watch out for global variables. Your playbutton is not local so you may want to define it with local.

Change your code to:

 local playbutton = display.newImage("ene5.png")
 playbutton.x = 190
 playbutton.y = 400
 playbutton:addEventListener( "touch", back )
 group:insert(playbutton)  --<------ must do this for storyboard to be able manage the assets
uid: 190375 topic_id: 34428 reply_id: 137005


[TOPIC: post.html]
#22

tore24

[GLOBAL: userInfoPane.html]
tore24
  • Contributor

  • 103 posts
  • Corona SDK

I just have one last question, if I want to re-create the scene, for example. In the code show:
SCENE1.LUA
local storyboard = require( "storyboard" )local scene = storyboard.newScene()  local playButton = display.newImage("ene5.png")local function back ()     storyboard.gotoScene( "scene2", "slideLeft", 800 )  --


The variable = 3, however, comes down to two by a function, if I want to scene1, the variable is still worth 2 and does not rise to 1, not if I explain. Any suggestions?
uid: 98258 topic_id: 34428 reply_id: 137006


[TOPIC: post.html]
#23

tore24

[GLOBAL: userInfoPane.html]
tore24
  • Contributor

  • 103 posts
  • Corona SDK

not reset the scene
uid: 98258 topic_id: 34428 reply_id: 137007


[TOPIC: post.html]
#24

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,605 posts
  • Enterprise

@aitor.10794 please move line #3 inside your createScene() function. The way Lua loads modules, that will create the image, but if this scene ever gets purged due to low memory or some other reason, that code at the top will NOT be re-executed. You should not create anything outside of either createScene() or enterScene() and you should not initialize any variable outside of createScene() or enterScene() that would need to be initialized if that scene gets removed for some reason.

It's okay to have functions that create things and do things outside of createScene() and enterScene() as long as those functions are called from inside of createScene() or enterScene();

uid: 199310 topic_id: 34428 reply_id: 137019


[TOPIC: post.html]
#25

Tim @ Playful Media

[GLOBAL: userInfoPane.html]
Tim @ Playful Media
  • Observer

  • 21 posts
  • Corona SDK

@aitor.10794 - Are you saying you want the value in "variable" to be passed from scene to scene?

If that is the case you could use Parameter Passing or take advantage of the fact that you will be requiring Storyboard for each of your scenes and pass the data through Storyboard - details here: http://www.coronalabs.com/blog/2012/08/07/managing-state-between-scenes/

In each scene during createScene you can access the valus, update as needed and save them back to your Storyboard variable or pass it along.
uid: 190375 topic_id: 34428 reply_id: 137023



[topic_controls]
Page 1 of 2 1 2
 
[/topic_controls]