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:
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:
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.
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
There are different techniques for passing data between scenes. Common ones include:
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:
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.
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