Yeris
Yeris

Reputation: 59

LUA/Corona sdk: storyboard.gotoScene() not working

I'm new to Corona and Lua, so pleas be patient. So i have a main screen, that I go to from my main file, and it works fine. Then i want to go back to that sceen from a diffrent sceen, and nothing happens (but the "win" is spaming my consol window).

The win function:

function win()  
    print("win!");
    storyboard.gotoScene( "MSCREEN.mscreen" );
end

Function where i'm calling it:

function scene:enterFrame(inEvent)
  --some other stuff
  if (ball.x>display.contentWidth and failFlag==false) then
    win();
  end

 end

and my main sceen:

local scene = storyboard.newScene();
local bg;
local text;

function scene:createScene(inEvent)
    bg = display.newImage( "MSCREEN/bg.png");
    bg.x = display.contentWidth / 2;
    bg.y = display.contentHeight / 2;

    text=display.newText( "Touch!", 0,0, fontName, 70);
    text.x =display.contentWidth / 2 ;
    text.y =display.contentHeight / 2 +230;  

    self.view:insert(bg);
    self.view:insert(text);
end

function scene:enterFrame(inEvent)
 tekstBujany(text);
end

function scene:touch(inEvent)   
    if inEvent.phase == "ended" then  
        storyboard.gotoScene( "JUMP.GJump" );       
    end
end

function scene:destroyScene(inEvent)
bg:removeSelf();
bg=nil;

text:removeSelf();
text=nil;
end -- End destroyScene().

scene:addEventListener("createScene", scene);
scene:addEventListener("destroyScene", scene);
Runtime:addEventListener("enterFrame", scene);
Runtime:addEventListener("touch",scene);

return scene;

SOLVED so adding

storyboard.purgeOnSceneChange = true;

did the trick, not sure why.

Upvotes: 0

Views: 1086

Answers (2)

Oliver
Oliver

Reputation: 29483

The "enterFrame" event gets generated at every frame for the Runtime object, so about 30 to 60 times / sec. So win() will be called at every frame if possible. Since win() causes a transition to a new scene, if storyboard.purgeOnSceneChange = false then enterFrame gets called for each scene, leading to constant stream of win() calls and constant stream of transitions to the same scene "MSCREEN.mscreen" (this can't be good). OTOH if you set storyboard.purgeOnSceneChange = true then the previous scene, here the one with enterFrame that calls win(), is purged so win() will only get called once.

In conclusion, setting storyboard.purgeOnSceneChange = true may work but I think there is a problem with the logic you have there: a scene transition should not be called at every frame! If you are monitoring for some condition to declare a win, you should either remove the enterFrame callback in win() or ensure the condition is false after win() called once:

function scene:enterFrame(inEvent)
  if (ball.x > display.contentWidth and failFlag == false) then
    win();
    Runtime:removeEventListener("enterFrame", scene);
  end
end

With the guard variable it looks like this:

local winCalled = false

function win()
    winCalled = true
    ...
end

function scene:enterFrame(inEvent)
  if (ball.x > display.contentWidth and failFlag == false) then
    win();
  end
end

Upvotes: 1

Fayer
Fayer

Reputation: 69

Be careful with the win() function, it looks like it gets called a lot of times. Set another variable and change it in the method.

function win()
    isNotWin = false -- so this method doesn't get called a lot of times.
    print("win!")
    storyboard.gotoScene( "MSCREEN.mscreen" )
end

function scene:enterFrame(inEvent)
    --some other stuff
    if (ball.x>display.contentWidth and failFlag==false and isNotWin == true) then
        win();
    end

end

Upvotes: 2

Related Questions