使用Corona SDK使用故事板重新启动游戏并获得一个奇怪的错误

时间:2014-02-19 01:05:51

标签: sdk lua corona uistoryboard

我目前正在Corona制作一款游戏,它有三个不同的场景,包括菜单,游戏和最终游戏场景。在游戏中,当你触摸它时,你可以在屏幕上上下移动你的精灵,它在第一次播放时工作得很好。但是当你撞到某个东西并进入重启屏幕并再次加载游戏时,当你触摸屏幕时,精灵几乎没有向上移动,如果你松开它会很快掉到底部,你无法将它推回去再次触摸屏幕。关于为什么会出现这个问题的任何想法?

local storyboard = require( "storyboard" )
local scene = storyboard.newScene()

-- ================================= --
-- required library files below here --
---------------------------------------
local physics = require("physics");
physics.start(false);
physics.setGravity( 0, 9.8 )

require("mobdebug").start();
---------------------------------------
-- required library files above here --
-- ================================= --


-- ================================ --
-- local vars/references below here --
--------------------------------------

local gameBg;
local cloud;
local cloud2;
local bird;
local crashedBird;
local topBar;
local bottomBar;
local topOfScreen;
local bottomOfScreen;
local group;
_W = display.viewableContentWidth
_H = display.viewableContentHeight


--------------------------------------
-- local vars/references above here --
-- ================================ --


-- =================================== --
-- functions for this scene below here
-----------------------------------------


-----------------------------------------
-- functions for this scene above here
-- =================================== --



------------------
--
--  birdReady
--
------------------
function birdReady()
    bird.bodyType = "dynamic";
end

------------------
--
--  end birdReady
--
------------------

------------------
--
--  activateBird
--
------------------
function activateBird(self,event)
    self:applyForce(0, -0.5, self.x, self.y);
end
------------------
--
--  End activateBird
--
------------------

------------------
--
--  Touch Screen Function
--
------------------
function touchScreen(event)
    if(event.phase == "began") then
        bird.enterFrame = activateBird;
        Runtime:addEventListener("enterFrame",bird);
    end

    if(event.phase == "ended") then
        Runtime:removeEventListener("enterFrame", bird);
    end
    return true;
end
-----------------
--
--  TouchScreen End
--
-----------------

-----------------
--
--  gameOver()
--
-----------------
function gameOver()

    bird.collided = false;
    storyboard.gotoScene("menu",  { effect = defaultTransition });
  --physics.setGravity( 0, 9.8 )

end
-----------------
--
--  end gameOver()
--
-----------------


-----------------
--
--  crashed()
--
-----------------
function crashed()
    crashedBird.x = bird.x;
    crashedBird.y = bird.y;
    crashedBird.isVisible = true;
    bird.bodyType = "static";
    bird.isVisible = false;
    gameOver();

end
-----------------
--
--  end crashed()
--
-----------------

-----------------
--
--  onCollision()
--
-----------------
function onCollision(event)
    if(event.phase == "began") then
        if(bird.collided == false) then
            bird.collided = true;
            crashed();
        end
    end
end
-----------------
--
--  end onCollision()
--
-----------------

-----------------
--
--Event Listener for touch
--
-----------------
Runtime:addEventListener("touch", touchScreen);

-----------------
--
--  End event Listener for touch
--
-----------------

-- Called when the scene's view does not exist:
function scene:createScene( event )
    group = self.view

    -- CREATE display objects and add them to 'group' here.




    -------------------
    --
    --  Bird
    --
    -------------------
    bird = display.newImage("images/bird.png");
    bird.x = (_W / 2) - 90;
    bird.y = _H / 2;
    bird.xScale = 0.7;
    bird.yScale = 0.8;
    bird.collided = false;


    physics.addBody(bird,"static", {density = 0.1, friction = 0.5, bounce = 0.1, radius = 12});

    group:insert(bird);

    birdIntro = transition.to(bird, {time = 2000, x = 100, onComplete = birdReady});
    --------------------
    --
    -- End Bird
    --
    --------------------

    --------------------
    --
    --  Crashed Bird
    --
    --------------------
    crashedBird = display.newImage("images/crashed.png");
    crashedBird.x = 100;
    crashedBird.y = 100;
    crashedBird.isVisible = false;
    group:insert(crashedBird);
    --------------------
    --
    --  End Crashed Bird
    --
    --------------------



end


-- Called immediately after scene has moved onscreen:
function scene:enterScene( event )
    local group = self.view

    globalGroup = display.newGroup();
    group:insert(globalGroup);


    -- INSERT code here (e.g. start timers, load audio, start listeners, etc.)

    --storyboard.purgeScene("menu");
    --Purge the end game file if added

    storyboard.loadScene("menu");

    --Runtime listener for the touch on the screen
    Runtime:addEventListener("touch", touchScreen);

    --Moves the cloud from right to left
    cloud.enterFrame = scrollCloud;
    Runtime:addEventListener("enterFrame", cloud);

    --Moves the cloud from right to left
    cloud2.enterFrame = scrollCloud;
    Runtime:addEventListener("enterFrame", cloud2);

    --Moves the top barricade from right to left
    topBar.enterFrame = scrollCloud;
    Runtime:addEventListener("enterFrame", topBar);

    --Moves the bottom barricade from right to left
    bottomBar.enterFrame = scrollCloud;
    Runtime:addEventListener("enterFrame", bottomBar);

    --Detects the collision
    Runtime:addEventListener("collision", onCollision);


end


-- Called when scene is about to move offscreen:
function scene:exitScene( event )
    local group = self.view



  bird:removeSelf();


 gameBg:removeSelf();
 cloud:removeSelf();
 cloud2:removeSelf();

 crashedBird:removeSelf();
 topBar:removeSelf();
 bottomBar:removeSelf();
 topOfScreen:removeSelf();
 bottomOfScreen:removeSelf();
 group:removeSelf();

    -- INSERT code here (e.g. stop timers, remove listeners, unload sounds, etc.)

    Runtime:removeEventListener("touch", touchScreen);

    Runtime:removeEventListener("enterFrame", bird);

    Runtime:removeEventListener("enterFrame", cloud);

    Runtime:removeEventListener("enterFrame", cloud2);

    Runtime:removeEventListener("enterFrame", topBar);

    Runtime:removeEventListener("enterFrame", bottomBar);

    Runtime:removeEventListener("collision", onCollision);
    --storyboard.removeAll ( )

end


-- Called prior to the removal of scene's "view" (display group)
function scene:destroyScene( event )
    local group = self.view
  --storyboard.removeAll();
    -- INSERT code here (e.g. remove listeners, widgets, save state, etc.)


  --bird:removeSelf();

    -- INSERT code here (e.g. stop timers, remove listeners, unload sounds, etc.)
    Runtime:removeEventListener("touch", touchScreen);

    Runtime:removeEventListener("enterFrame", bird);

    Runtime:removeEventListener("enterFrame", cloud);

    Runtime:removeEventListener("enterFrame", cloud2);

    Runtime:removeEventListener("enterFrame", topBar);

    Runtime:removeEventListener("enterFrame", bottomBar);

    Runtime:removeEventListener("collision", onCollision);
  group = nil;
end



---------------------------------------------------------------------------------
-- END OF YOUR IMPLEMENTATION
---------------------------------------------------------------------------------

-- "createScene" event is dispatched if scene's view does not exist
scene:addEventListener( "createScene", scene )

-- "enterScene" event is dispatched whenever scene transition has finished
scene:addEventListener( "enterScene", scene )

-- "exitScene" event is dispatched before next scene's transition begins
scene:addEventListener( "exitScene", scene )

-- "destroyScene" event is dispatched before view is unloaded, which can be
-- automatically unloaded in low memory situations, or explicitly via a call to
-- storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( "destroyScene", scene )


---------------------------------------------------------------------------------

return scene

1 个答案:

答案 0 :(得分:0)

听起来我觉得问题是在退出到下一个场景之前没有删除所有侦听器的问题。确保清理所有变量,并正确地结束所有事件监听器。我个人并不熟悉故事板。如果你不能让它工作,我建议不要使用任何东西,只需要一个main.lua,只需要从main中调用它们的函数就可以获得其他lua文档。可能会更容易自己处理错误清理。