Hey,
I am starting to write this very simple 2-screen app, I have two issues:
- The only piece of modifiable data I have gets passed from scene 1 (where it gets modified) to scene 2 only the first time i go to scene2. In other words if it gets modified in scene-1 another time I find the same old value when getting back to scene2.
I guess that’s because scene2 doesn’t get destroyed and displays the same as the first time it was called…
How do I get around that, to pass changing variables among scenes??
- I pass from one scene to the other by using a button that triggers a sliding transition (two identical buttons placed in the same screen portion in the two scenes), but If re-click it before the transition ends I got a black screen-app crash. Why is that???
CODE scene1:
local composer = require( "composer" ) local scene = composer.newScene() current = 0 -- THIS IS THE VARIABLE IN QUESTION, HERE I SET IT TO THE INITIAL VALUE OF '0' function scene:create( event ) --- THIS NEXT PART IS TO SET THE DISPLAY OBJECTS, YOU CAN SKIP THIS local sceneGroup = self.view local background = display.newImageRect ("background\_01.png", 360, 570) background.x = centerX background.y = centerY sceneGroup:insert(background) local textNumber = display.newText ( "0", centerX, centerY - 120, nil , 50 ) sceneGroup:insert(textNumber) local function adding () -- THIS IS THE FIRST FUNCTION THAT OPERATES ON THE VARIABLE... current = current + 1 textNumber.text = current end local plusButton = display.newCircle( centerX, centerY + 20, 30 ) sceneGroup:insert(plusButton) plusButton:addEventListener( "tap", adding ) local function subbing () ---- THIS IS THE SECOND FUNCTION THAT MODIFY THE VALUE OF THE VARIABLE current = current - 1 textNumber.text = current end local minusButton = display.newRect( centerX + 80, centerY + 140 , 50, 20 ) sceneGroup:insert(minusButton) minusButton:addEventListener( "tap", subbing ) local function refresh() --- AND THIS IS THE THIRD FUNCTION THAT RESET THE VALUE TO '0' current = 0 textNumber.text = current end local resetButton = display.newRect( centerX - 80, centerY + 140 , 50, 50 ) sceneGroup:insert(resetButton) resetButton:addEventListener( "tap", refresh ) local function toSave() ---- HERE I GET TO SCENE2.... composer.gotoScene ("save", "slideRight") end local toSaveButton = display.newRect( centerX, 570, 360, 200 ) ---- .... BY CLICKING THIS BUTTON sceneGroup:insert(toSaveButton) toSaveButton:addEventListener( "tap", toSave ) end
CODE scene2:
local composer = require( "composer" ) local scene = composer.newScene() function scene:create( event ) local sceneGroup = self.view local function toCounter() composer.gotoScene ("Screen1", "slideLeft") end local bg = display.newImage( "bg4.png" ) sceneGroup:insert(bg) bg.x = centerX bg.y = centerY local toCounterButton = display.newRect( centerX, 570, 360, 200 ) --- THIS IS THE TWIN BUTTON THAT SENDS BACK TO SCENE 1 sceneGroup:insert(toCounterButton) toCounterButton:addEventListener( "tap", toCounter ) local currentDisplayer = display.newText("Current: " .. current, centerX, centerY - 120, nil , 50 ) ---- HERE IS WHERE I'D WANT THE VARIABLE ('current') TO BE DISPLAYED INSIDE OF THE TEXT BUT IT ONLY DOES ON UPON THE FIRST LAOD OF THIS SCENE, WITHOUT UPDATING THE VALUE WHEN IT GETS MODIFEID IN SCENE 1 sceneGroup:insert(currentDisplayer) end
(The other phases of the composer templates I left untouched in both scenes)
Any guess?
Thanks!!!