Atrag
Atrag

Reputation: 743

scene:show runs twice after going to scene the second time

I go from menu.lua to game.lua using gotoscene. At the end of game.lua I transition from game.lua to menu.lua again. Under scene:show in menu.lua I remove the game scene. When I then move back to game.lua everything in scene:show is repeated twice. scene:create is still only once.

Any idea why this would be?

Thank you.

function scene:show( event )

    local sceneGroup = self.view
    local phase = event.phase

    if ( phase == "will" ) then

print("does this print once or twice?")



        -- Code here runs when the scene is still off screen (but is about to come on screen)

    elseif ( phase == "did" ) then
    physics.start()
gameLoopTimer = timer.performWithDelay( 1750, gameLoop, 0 )




  -- Start the music!
        audio.play( musicTrack, { channel=1, loops=-1 } )
        audio.setVolume( 0, { channel=1 } ) 


        -- Code here runs when the scene is entirely on screen


    end

end

composer.gotoscene(game.lua) is called by a tap one of the multiple objects created by this the menuDrawer function that is called during the create phase.

local function menuDrawer()

....

for i = 1, #menuLetters, 1 do 

....

    local Letterbackground = display.newRoundedRect(sceneGroup, Letterbackgroundx, Letterbackgroundy, 100, 125, 10 )

....
Letterbackground:addEventListener( "tap", gotoGame )
end

The EventListener is never removed as there are only defined by the local variable within the function. Would this cause the problem?

If you need any more information please let me know.

Upvotes: 0

Views: 326

Answers (2)

Atrag
Atrag

Reputation: 743

Problem solved. The gotoscene in menu.lua was in a functioned called by tapping and it was fixed by 'return true' at the end of the function.

Upvotes: 1

ldurniat
ldurniat

Reputation: 1702

The scene:show have two phase equal did and will. So it is called twice indeed. See code below (it comes from Introducing the Composer API)

-- "scene:show()"
function scene:show( event )

   local sceneGroup = self.view
   local phase = event.phase

   if ( phase == "will" ) then
      -- Called when the scene is still off screen (but is about to come on screen).
   elseif ( phase == "did" ) then
      -- Called when the scene is now on screen.
      -- Insert code here to make the scene come alive.
      -- Example: start timers, begin animation, play audio, etc.
   end
end

Upvotes: 1

Related Questions