problem: Composer functions called multiple times, please help

Hello

I recently moved to composer library from the director, so far all seem good, but one thing.

I inserted a couple of print commands in my code so i can track a certain problem with a transition beginning in one of the scenes. When i did that i realized the functions of the composer template are called more then once.

although the ‘will’ and ‘did’ inside them are called once. This happens in all scenes causing some functions i run at the ‘will’ part to perform some weird stuff.  

When I try to ‘composer.removeScene(“scene name”)/composer.removeHidden()’ - it seem to have almost no effect. Any help will be appreciated. 

Here is a sample code from one of my scenes:

------------------------------------------------------------------------------ local composer = require( "composer" )

local scene = composer.newScene()

local rank;

local hatTransition1 = 0;

local function startRank()


rank = display.newImage(“imageName”);

rank.x = rankBG.x;

rank.y = rankBG.y;

statsGroup:insert(rank);

local function sizeHatX()

local size = 0.9;

local function go()

  hatTransition1 = transition.to(rank, {time=1400, xScale=1, transition=easing.inOutQuad, onComplete=sizeHatX} );

end

  hatTransition1 = transition.to(rank, {time=1400, xScale=size, transition=easing.inOutQuad, onComplete=go} );

end


end

local bg = display.newImage(“imageName”);

bg .x = _W/2;

bg .y = _H/2;

skins[1] = display.newImage(“imageName”);

skins[1].x = bg.x - 100;

skins[1].y = bg.y - 135;

skins[2] = display.newImage(“imageName”);

skins[2].x = bg.x - 100;

skins[2].y = bg.y - 61;

local localGroup = display.newGroup();
localGroup:insert(bg);
localGroup:insert(skins[1]);
localGroup:insert(skins[2]);

function scene:create( event )    local sceneGroup = self.view       sceneGroup:insert(localGroup); end – “scene:show()” function scene:show( event )    print(“there?”);    local sceneGroup = self.view    local phase = event.phase    if ( phase == “will” ) then startRank();    elseif ( phase == “did” ) then    end end function scene:hide( event )     local sceneGroup = self.view     local phase = event.phase     local parent = event.parent  --reference to the parent scene object     if ( phase == “will” ) then if(hatTransition1~=0) then   transition.cancel(hatTransition1); end elseif(phase == “did”) then     end end – Back button scene:addEventListener( “create”, scene ) scene:addEventListener( “show”, scene ) scene:addEventListener( “hide”, scene ) return scene

The ‘savedData = require(“savedData”);’ is called at the ‘main’ scene and is a global so i can access it from all scenes.

Ok nevermind, found out what was my mistake:

I did not follow the objects.addEventListener() with objects.removeEventListener() when i exit the scenes

what caused  the next scene to be called multiple times.

Those little thing that vanish from your eyes…

thanks anyway.

have a good day

I’ve just today solved a similar problem, that I thought I’d mention here in case it helps anyone…

To replay my game, I’d been using a ‘play’ button, created with widget.newButton(…). In the button’s onRelease handler I was calling:

composer.gotoScene("level1", {effect="fromBottom", time=500})

I didn’t realise that a player might tap the button twice quickly (while the 500 ms transition is happening) and this would result in two gotoScene commands executing concurrently. This was causing the “level1” scene to begin to load, then unload, then load again – and the results were very strange and hard to debug.

Now inside the onRelease handler I’ve got:

playBtn:setEnabled(false) -- Make sure the button is not tapped twice (triggering two scene changes) composer.gotoScene("level1", {effect="fromBottom", time=500})

This now seems to work properly.

Yep, ensure that you remove all touch/tap event listeners before changing scenes so that the player doesn’t accidentally break your game.

Yeah, I agree with both of you. This can be very frastrating and can cause time to solve (along many other problems and errors he encounter along the way, hey :wink: , I tell you we should have our own rights…)

Did not realize the transition window for two/multiple touch can happen, now I need to fix that too :slight_smile:

Oh well, thanks men.

Ok nevermind, found out what was my mistake:

I did not follow the objects.addEventListener() with objects.removeEventListener() when i exit the scenes

what caused  the next scene to be called multiple times.

Those little thing that vanish from your eyes…

thanks anyway.

have a good day

I’ve just today solved a similar problem, that I thought I’d mention here in case it helps anyone…

To replay my game, I’d been using a ‘play’ button, created with widget.newButton(…). In the button’s onRelease handler I was calling:

composer.gotoScene("level1", {effect="fromBottom", time=500})

I didn’t realise that a player might tap the button twice quickly (while the 500 ms transition is happening) and this would result in two gotoScene commands executing concurrently. This was causing the “level1” scene to begin to load, then unload, then load again – and the results were very strange and hard to debug.

Now inside the onRelease handler I’ve got:

playBtn:setEnabled(false) -- Make sure the button is not tapped twice (triggering two scene changes) composer.gotoScene("level1", {effect="fromBottom", time=500})

This now seems to work properly.

Yep, ensure that you remove all touch/tap event listeners before changing scenes so that the player doesn’t accidentally break your game.

Yeah, I agree with both of you. This can be very frastrating and can cause time to solve (along many other problems and errors he encounter along the way, hey :wink: , I tell you we should have our own rights…)

Did not realize the transition window for two/multiple touch can happen, now I need to fix that too :slight_smile:

Oh well, thanks men.