0

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
greatwolf
  • 20,287
  • 13
  • 71
  • 105
Saint Dee
  • 985
  • 6
  • 21
  • 43

1 Answers1

0

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

Adam Storm
  • 724
  • 1
  • 6
  • 13
  • I have done this and have also removed the jet's eventlistener in the exitScene but result is the same. – Saint Dee Feb 26 '14 at 06:33
  • You can always just use storyboard.removeScene("game"), that is sure to work while you iron out the purgeScene issues you are having. – Adam Storm Feb 26 '14 at 17:06