Help needed to restart my game...

SCENE 1:

here i have 2 object which removeSelf() when the character collides with them. When both the objects are gone, it goes to the second scene.

SCENE 2: Restart Scene

by clicking here you go back to the game scene, and i have used storyboard.purgeScene(“game”) in this scene.

when the game start again and i collide the object with my character an error occurs.

“The object has already been removed from the stage or a parent or ancestor has already been removed”

its referring to my removeSelf() method…

How do I fix this…any other alternatives ?

This is the code…

GAME.lua
 

display.setStatusBar(display.HiddenStatusBar)

local physics = require “physics”

physics.start()

physics.setGravity(0,0)

local storyboard = require (“storyboard”)

local scene = storyboard.newScene()

function scene:createScene(event)

    screenGroup = self.view

    

    

    background =  display.newImage(“bg.jpg”)

    screenGroup:insert(background)

    

    

    character = display.newImage(“character.png”)

    character.x = 100

    character.y = 100

    physics.addBody(character, “dynamic”)

    screenGroup:insert(character)

    

    obj1 = display.newImage(“obj.png”)

    obj1:translate( 200, 100 )

    physics.addBody(obj1, “static”)

    

    objGroup = self.view

    objGroup:insert(obj1)

    

    obj2 = display.newImage(“obj.png”)

    obj2:translate( 500, 100 )

    physics.addBody(obj2, “static”)

    objGroup:insert(obj2)

end

  

    --RESPOND TO TOUCH SCREEN EVENT

    function touchScreen(event)

        

        if event.phase == “began” then

            transition.to(character,{time=1000, x=event.x, y=event.y})

        end

    end

    

    --THE EVENT FUNTION ONCOLLISION

    function onCollision(self, event)

        

        self:removeSelf()

        if objGroup.numChildren < 3 then

            storyboard.gotoScene(“restart”,“flip”)

        end        

    end    

function scene:enterScene(event)

   

    storyboard.purgeScene(“restart”)

   Runtime:addEventListener(“touch”, touchScreen)

    

    --CALLING THE ONCOLLISION EVENT

    obj1.collision = onCollision

    obj1:addEventListener(“collision”, obj1)

    

    obj2.collision = onCollision

    obj2:addEventListener(“collision”, obj2)

end

function scene:exitScene(event)

end

function scene:destroyScene(event)

end

scene:addEventListener(“createScene”,scene)

scene:addEventListener(“enterScene”,scene)

scene:addEventListener(“exitScene”,scene)

scene:addEventListener(“destroyScene”,scene)

return scene

RESTART.lua

display.setStatusBar(display.HiddenStatusBar)

local storyboard = require (“storyboard”)

local scene = storyboard.newScene()

function scene:createScene(event)

    local screenGroup = self.view

        

    reset =  display.newImage(“reset.png”)

    screenGroup:insert(reset)

end

–RESPOND TO TOUCH SCREEN EVENT

function touchScreen(event)

    if event.phase == “began” then

        storyboard.gotoScene(“game”,“fade”,400)

    end

end

function scene:enterScene(event)

   

    storyboard.purgeScene(“game”)

    

   reset:addEventListener(“touch”, touchScreen)

end

function scene:exitScene(event)

    reset:removeEventListener(“touch”, touchScreen)

end

function scene:destroyScene(event)

end

scene:addEventListener(“createScene”,scene)

scene:addEventListener(“enterScene”,scene)

scene:addEventListener(“exitScene”,scene)

scene:addEventListener(“destroyScene”,scene)

return scene

Can you post your code please? It’s impossible to tell you exactly what the problem is if we can’t see it for ourselves.

Can you post your code please? It’s impossible to tell you exactly what the problem is if we can’t see it for ourselves.