LUA/Corona sdk: storyboard.gotoScene() 不起作用。

我是 Corona 和 Lua 的新手,请耐心等待。

我有一个主屏幕,我从我的主文件中进入它,它能正常工作。然后我想从另一个屏幕返回该屏幕,但是什么都没有发生(除了“win!”在我的控制台窗口不停地输出)。

胜利函数:

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

我调用它的函数:

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

 end

我的主屏幕:

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;

已解决,所以添加

storyboard.purgeOnSceneChange = true;

解决了问题,不确定为什么。

点赞
用户3183373
用户3183373

注意 win() 函数,它似乎被调用了很多次。设置另一个变量并在方法中进行更改。

function win()
    isNotWin = false -- 这样这个方法就不会被频繁调用。
    print("win!")
    storyboard.gotoScene( "MSCREEN.mscreen" )
end

function scene:enterFrame(inEvent)
    --一些其他的操作
    if (ball.x>display.contentWidth and failFlag==false and isNotWin == true) then
        win();
    end
end
2014-01-12 21:20:18
用户869951
用户869951

"enterFrame"事件在Runtime对象中的每一帧都会生成,约为30到60次/秒。因此如果可能,每一帧都会调用win()。由于win()会导致转换到一个新场景,如果storyboard.purgeOnSceneChange = false,那么对于每一个场景,都会调用enterFrame,导致win()的不断流和不断转换到相同的场景“MSCREEN.mscreen”(这不是好的情况)。另一方面,如果你将storyboard.purgeOnSceneChange = true,则之前的场景,在这里调用enterFrame调用win(),被清除,所以win()只会被调用一次。

总之,设置storyboard.purgeOnSceneChange = true可能会有用,但我觉得你的逻辑存在问题:场景转换不应该在每一帧被调用!如果你正在监视某个条件来宣布胜利,你应该在win()中删除enterFrame回调或确保win()被调用一次后条件为假:

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

带有保护变量的示例如下:

local winCalled = false

function win()
    winCalled = true
    ...
end

function scene:enterFrame(inEvent)
  if (ball.x > display.contentWidth and failFlag == false) then
    win();
  end
end
2014-01-13 04:30:22