2 matters about Composer

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!!!

I don’t see where you are calling “return scene” at the end of the scene?  I don’t see where you are setting up the event handlers for the various composer events.

You should not remove any lines of code from the scene template as they all serve a purpose.

Hi,

I didn’t include those in the quote 'cause, as I wrote at bottom of the post, I left completely untouched as they come in the template (which I obviously included in my project in its entirety)…

A couple of things. 

“current” is a global variable.  But more importantly it’s in the “Main chunk” of that lua module.  Therefore “current = 0” only gets executed once, unless the scene is completely unloaded.  You would have to remove the scene completely to get that line of code to execute when the scene is re-entered.  The “right” way to reset it, is to put code in your scene’s “show()” event, as part of the  “will” phase:

function scene:show( event )

      if event.phase == “will” then

            count = 0

      end

end

You should “reset” things there.

Secondly I can’t really see where you’re defining “textNumber”.  It looks like you’re creating “currentDisplayer” inside the create function of scene2.  That won’t get re-executed either until the scene has been removed or recycled.  

Rob

Hi Rob,

Thanks for the answer.

Everything is running as I want it to in scene1, that’s why I keep the code in ‘create scene’, so that everything in the scene is set and ready everytime I get back to it.

The problem is in scene2 where the ‘current’ value doesn’t get updated to what is set in scene1. As you rightly observed in this case it’s because of the positioning of the code in ‘create scene’ of scene2. But as I tried to move the ‘currentDisplayer’ object to be generated in ‘scene show’ (will), the value gets  actually updated, though everytime I get back to scene2 another text is created over the former one… Even placing a ‘display.remove(currentdisplayer)’   in ‘scene(2) hide’ doesn’t make the former text object to go away. What do you think about this???

I don’t see where you are calling “return scene” at the end of the scene?  I don’t see where you are setting up the event handlers for the various composer events.

You should not remove any lines of code from the scene template as they all serve a purpose.

Hi,

I didn’t include those in the quote 'cause, as I wrote at bottom of the post, I left completely untouched as they come in the template (which I obviously included in my project in its entirety)…

A couple of things. 

“current” is a global variable.  But more importantly it’s in the “Main chunk” of that lua module.  Therefore “current = 0” only gets executed once, unless the scene is completely unloaded.  You would have to remove the scene completely to get that line of code to execute when the scene is re-entered.  The “right” way to reset it, is to put code in your scene’s “show()” event, as part of the  “will” phase:

function scene:show( event )

      if event.phase == “will” then

            count = 0

      end

end

You should “reset” things there.

Secondly I can’t really see where you’re defining “textNumber”.  It looks like you’re creating “currentDisplayer” inside the create function of scene2.  That won’t get re-executed either until the scene has been removed or recycled.  

Rob

Hi Rob,

Thanks for the answer.

Everything is running as I want it to in scene1, that’s why I keep the code in ‘create scene’, so that everything in the scene is set and ready everytime I get back to it.

The problem is in scene2 where the ‘current’ value doesn’t get updated to what is set in scene1. As you rightly observed in this case it’s because of the positioning of the code in ‘create scene’ of scene2. But as I tried to move the ‘currentDisplayer’ object to be generated in ‘scene show’ (will), the value gets  actually updated, though everytime I get back to scene2 another text is created over the former one… Even placing a ‘display.remove(currentdisplayer)’   in ‘scene(2) hide’ doesn’t make the former text object to go away. What do you think about this???