How to transfer health between scenes? and scenes overlap

 local lives = 5 -- Called when the scene's view does not exist: function scene:createScene( event ) local group = self.view local question = display.newText ("WNumber 1 is the correct number?", 0, 0, native.systemFont, 18) question.x = display.contentCenterX question.y = display.contentCenterY local livesLabel = display.newText("Lives:" ..tostring(lives), 10, 30, native.systemFont, 16) livesLabel.x = display.contentCenterX livesLabel.y = display.contentCenterY -100 local isCorrect = display.newText ("Number 1", 0, 0, native.systemFont, 18) isCorrect.x = display.contentCenterX isCorrect.y = display.contentCenterY + 100 local bgImage1 = display.newText ("Number 2", 0, 0, native.systemFont, 18) bgImage1.x = display.contentCenterX bgImage1.y = display.contentCenterY + 80 local bgImage2 = display.newText ("Number 3", 0, 0, native.systemFont, 18) bgImage2.x = display.contentCenterX bgImage2.y = display.contentCenterY + 120 local bgImage = display.newText ("Number 4", 0, 0, native.systemFont, 18) bgImage.x = display.contentCenterX bgImage.y = display.contentCenterY + 140 -- local livesLabel = display.newText("Lives:" ..tostring(lives), 10, 30, native.systemFont, 16) -- livesLabel.x = display.contentCenterX +100 -- livesLabel.y = display.contentCenterY - 200 local function isCorrectTap( event ) storyboard.gotoScene("levels.question2") print(" Next question") end isCorrect:addEventListener("tap", isCorrectTap ) group:insert( isCorrect ) local function bgTap( event ) lives = lives - 1 livesLabel.text = "Lives:" ..tostring(lives) print("Lives Left:" .. lives) end bgImage:addEventListener("tap", bgTap ) bgImage1:addEventListener("tap", bgTap ) bgImage2:addEventListener("tap", bgTap ) group:insert( bgImage ) end

When I made a new file with pretty much the same thing as this one. and did storyboard.gotoScene(“levels.question2”).

When I press the button that goes to the Scene question2. The scene overlaps the original scene.  The original scene doesn’t disappear. 

2nd problem. Right now if you press the wrong option. It decreases the lives value by 1. So I press the wrong answer twice. I would have 3 lives left. After I get the question right and go to the scene question2 How would I be able use the number of lives left over from the first scene in the 2nd scene?

There are different techniques for passing data between scenes.  Common ones include:

  1. Adding data elements to the storyboard table.  You can add a table .state to the storyboard object and in any scene that “requires” storyboard, you can then access the data:

storyboard.state = {}

storyboard.state.health = health

then in the next scene just access:

print(storyboard.state.health)

  1. Use the storyboard parameter passing.

storyboard.gotoScene(“nextscene”, {time=500, effect=“fade”, params={health = health}})

or something like that, then in the next scene’s createScene or enterScene:

   local params = event.params

   print(params.health)

  1. Or use an external data module (probably the preferred way).  See:  http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

The problem with #1 is that it depends on having a .state table and it assumes we won’t ever decided to include our own .state entry in the object.  Now the good news is it won’t happen.  We’ve deperecated storyboard in favor if its more modern sibling “Composer”.  We wont’ be making any more changes to Storyboard, so it’s safe to do.  In Composer (which you should consider switching too if you’re not too far into), we have support for a setVariable() getVariable() feature which has a code safe way to use the scene manager to pass data.

#2 can only be accessed in createScene and enterScene.  If you need the data in exitScene() or outside of those two scene,s you have to make sure to copy the values while in createScene or enterScene.

#3 is the safest way.

Rob

Thanks for your reply.

I tried option 1 and 2 and both of them says nil as the value and I dont understand option 3

Option 3, as Rob Miracle said, is probably the best way.

Basically, you create a new file, and in that file make a blank table:

[lua]

local data = {}

data.health = 50 – Default value for health

return data

[/lua]

At the end, you’ll notice the return statement. That means, “when a file requires() this, give them this table”. Because of the way Lua works, every file that uses this one gets the same table.

[lua]

– Scene1.lua

local data = require(“mydata”) – Loads the data table

data.health = 40

[/lua]

[lua]

– Scene2.lua; loaded after Scene1

local data = require(“mydata”) – Now scene 1 and scene 2 have access to the same object

print(data.health) --> 40, because it’s accessing the health that was changed by scene 1

[/lua]

This is actually the same approach as option 1, but you create a new object to store data in instead of “piggyback” on the storyboard object :slight_smile:

  • C

Thanks Caleb. It worked

My other problem is that when I goto the next scene. The first scene doesn’t disappear and it seems to overlap.

Here is a screenshot of what it looks like

http://gyazo.com/fb1d1a88fd9508be316b504428bb911c

You are not inserting your text objects into the “group” so Storyboard can’t manage them, i.e.:

group:insert (isCorrect )

Rob

There are different techniques for passing data between scenes.  Common ones include:

  1. Adding data elements to the storyboard table.  You can add a table .state to the storyboard object and in any scene that “requires” storyboard, you can then access the data:

storyboard.state = {}

storyboard.state.health = health

then in the next scene just access:

print(storyboard.state.health)

  1. Use the storyboard parameter passing.

storyboard.gotoScene(“nextscene”, {time=500, effect=“fade”, params={health = health}})

or something like that, then in the next scene’s createScene or enterScene:

   local params = event.params

   print(params.health)

  1. Or use an external data module (probably the preferred way).  See:  http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

The problem with #1 is that it depends on having a .state table and it assumes we won’t ever decided to include our own .state entry in the object.  Now the good news is it won’t happen.  We’ve deperecated storyboard in favor if its more modern sibling “Composer”.  We wont’ be making any more changes to Storyboard, so it’s safe to do.  In Composer (which you should consider switching too if you’re not too far into), we have support for a setVariable() getVariable() feature which has a code safe way to use the scene manager to pass data.

#2 can only be accessed in createScene and enterScene.  If you need the data in exitScene() or outside of those two scene,s you have to make sure to copy the values while in createScene or enterScene.

#3 is the safest way.

Rob

Thanks for your reply.

I tried option 1 and 2 and both of them says nil as the value and I dont understand option 3

Option 3, as Rob Miracle said, is probably the best way.

Basically, you create a new file, and in that file make a blank table:

[lua]

local data = {}

data.health = 50 – Default value for health

return data

[/lua]

At the end, you’ll notice the return statement. That means, “when a file requires() this, give them this table”. Because of the way Lua works, every file that uses this one gets the same table.

[lua]

– Scene1.lua

local data = require(“mydata”) – Loads the data table

data.health = 40

[/lua]

[lua]

– Scene2.lua; loaded after Scene1

local data = require(“mydata”) – Now scene 1 and scene 2 have access to the same object

print(data.health) --> 40, because it’s accessing the health that was changed by scene 1

[/lua]

This is actually the same approach as option 1, but you create a new object to store data in instead of “piggyback” on the storyboard object :slight_smile:

  • C

Thanks Caleb. It worked

My other problem is that when I goto the next scene. The first scene doesn’t disappear and it seems to overlap.

Here is a screenshot of what it looks like

http://gyazo.com/fb1d1a88fd9508be316b504428bb911c

You are not inserting your text objects into the “group” so Storyboard can’t manage them, i.e.:

group:insert (isCorrect )

Rob