简体   繁体   中英

Corona SDK PurgeScene destroys physics

I have create a simple scrolling game, following closely to Mark Farkland's Game tutorial. I clearly followed his coding pattern but got to the point where I am just lost and don't why mine behaves different that his example. Everything is working fine until after a scene is purged.

I have two lua files: game.lua and gameover.lua. In game.lua exitScene I have removed all the eventlisteners. In gameover.lua enterScene, I have purged game.lua scene. When the user touched the screen it then loads game.lua file. Every objects load and refreshes like new apart from the main object which just shoots upwards.

Game.lua

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

    function scene:createScene(event)
         physics.start()
         ....
         local jet = display.newImage("images/jet_normal.png")
         physics.addBody(jet, "dynamic", {density=.07, bounce=0.1, friction=.2, radius=12})

        local activateJets = function(self, event)
            self:applyForce(0, -1.5, self.x, self.y)
        end

        function touchScreen(event)
            local phase = event.phase
            if phase == "began" then
                jet.enterFrame = activateJets
                Runtime:addEventListener("enterFrame", jet)
            end
            if phase == "ended" then
                Runtime:removeEventListener("enterFrame", jet)
            end
        end

        Runtime:addEventListener("touch", touchScreen)
end

function scene:exitScene(event)
    Runtime:removeEventListener("touch", touchScreen)
    Runtime:removeEventListener("enterFrame", enemy1)
    Runtime:removeEventListener("collision", onColission)
end

GameOver.lua

function start(event)
    if event.phase == "began" then
        storyboard.gotoScene("game", "fade", 100)
    end
end

function scene:enterScene(event)
    storyboard.purgeAll("game")
    background:addEventListener("touch", start)
end

function scene:exitScene(event)
    background:removeEventListener("touch", start)
end

You need to add the jet to the current group.

function scene:createScene(event)
   local group = self.view
   --...
   local jet = display.newImage("images/jet_normal.png")
   --...
   group:insert(jet)
   Runtime:addEventListener("touch", touchScreen)
end

The proper cleanup will happen to the group when exitScene is triggered.

more info / different implementation:

http://docs.coronalabs.com/guide/graphics/group.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM