Switching physical scenes

Hi,

This is the second post i’m making about this subject because I’m struggling with it for too long…

I have a project with physical objects that are constantly being added to a table.

I want to switch to a different scene and have these objects stop from reappearing.

What happens is that when I return to the original physical scene after switching, the table begins to refill with double pace - meaning the listeners for the function that adds the objects to the table have doubled.

I tried purging and removing the scene, deleting the table, removing the event listeners, removing all assets from the scene, but nothing works!

I’m using storyboard - i got createscene, enterscene, exitscene functions.

I still don’t completely understand where do I use each function? should I remove the assets on the exitscene of the scene I’m leaving, or inside the enterscene of the scene i’m entering?

This is all very confusing, I would appreciate some help around this…

Well it might help to see your code, but in general, you should create your objects in createScene(), activate them in enterScene(), Stop any activity in exitScene() and remove anything in destoryScene() that doesn’t automatically get removed.  enterScene() and exitScene() will always be in pairs.  And while it’s not safe to always say that there will be a destoryScene() for every createScene(), there will be a destroyScene() if createScene() needs called a 2nd time.

The first time a scene is called you will get:

createScene

enterScene

and if you leave… exitScene

If you come back to this scene and either a) have not called purgeScene() or removeScene() and there hasn’t been a low memory warning, then only

enterScene

exitScene (if you leave)

will be called.  If you call purgeScene() or removeScene() then you will get a:

destoryScene

And when you re-enter:

createScene

enterScene

exitScene, etc.

Without knowing how your spawning your objects and how you are removing them when you leave, it’s going to hard to be more specific.

I followed what you said, And i narrowed to problem to the “physics.stop/start” functions…

When I exit the physical scene for the 1st time, everything works properly. When I exit it for the 2nd time, I receive many “physics.start has not been called” logs and the objects continue to the other scene…

this is my code-

module(…,package.seeall);

local storyboard = require( “storyboard” )

local main = require “main”

local physics = require “physics”

local crate = require “crate”

local ball = require “ball”

local game = require “game”

local score = require “score”

local scene1= require “scene1”

local scene = storyboard.newScene()

function scene:createScene( event )

function scene_time_bg()

    game.gameStart()

    btn_ball = display.newImage(“Photos/Ball.png”)

    btn_ball.x=900

    btn_ball.y=560

    btn_ball.xScale=1.1

    btn_ball.yScale=1.1

    btn_ball:toFront()

    score.setCombo()

end

function scene_time_start()

            local timeref=90

            ball.startBall(btn_ball)

            score.countDown()

            local timeLastEnemy = 0 

            local timeLastBomb = 0 

                local function dropBomb()

                    function dropbombAction()

                        crate.newBomb()

                    end

                timer.performWithDelay(math.random(600, 800), dropbombAction);

                end

                local function dropBox()

                    function dropcrateAction()

                        crate.newCrate()

                    end

                timer.performWithDelay(math.random(500, 900), dropcrateAction);

                end

                local function gameLoop(event)

                    if event.time - timeLastEnemy >= math.random(400, 800) then

                        dropBox()

                        timeLastEnemy = event.time

                    if event.time - timeLastBomb >= math.random(1500,2000) then

                        dropBomb()

                    timeLastBomb = event.time

                    end

                end

            end

local time = 60

ScoreRef=score.showScore()

Runtime:addEventListener(“enterFrame”, gameLoop)

      function removeEvent()

                 Runtime:removeEventListener(“enterFrame”, gameLoop)

      end

   end

end

function scene:enterScene( event )

scene_time_bg()

physics.start()

timer.performWithDelay(1000,scene_time_start)

end

function scene:exitScene( event )

storyboard.removeScene(“scene_time”)

end

function scene:destroyScene( event )

physics.stop()

removeEvent()

crate.removeTables()

end

scene:addEventListener( “createScene”, scene )

scene:addEventListener( “enterScene”, scene )

scene:addEventListener( “exitScene”, scene )

scene:addEventListener( “destroyScene”, scene )

return scene

Clearly I’m doing something wrong…Can you tell me what that is?

If you are starting physics in enterScene, you have to stop or pause physics in exitScene.

But it still doesn’t work the second time,  as I said… :X

Are you adding your objects to the main storyboard group (scene.view)?

Well it might help to see your code, but in general, you should create your objects in createScene(), activate them in enterScene(), Stop any activity in exitScene() and remove anything in destoryScene() that doesn’t automatically get removed.  enterScene() and exitScene() will always be in pairs.  And while it’s not safe to always say that there will be a destoryScene() for every createScene(), there will be a destroyScene() if createScene() needs called a 2nd time.

The first time a scene is called you will get:

createScene

enterScene

and if you leave… exitScene

If you come back to this scene and either a) have not called purgeScene() or removeScene() and there hasn’t been a low memory warning, then only

enterScene

exitScene (if you leave)

will be called.  If you call purgeScene() or removeScene() then you will get a:

destoryScene

And when you re-enter:

createScene

enterScene

exitScene, etc.

Without knowing how your spawning your objects and how you are removing them when you leave, it’s going to hard to be more specific.

I followed what you said, And i narrowed to problem to the “physics.stop/start” functions…

When I exit the physical scene for the 1st time, everything works properly. When I exit it for the 2nd time, I receive many “physics.start has not been called” logs and the objects continue to the other scene…

this is my code-

module(…,package.seeall);

local storyboard = require( “storyboard” )

local main = require “main”

local physics = require “physics”

local crate = require “crate”

local ball = require “ball”

local game = require “game”

local score = require “score”

local scene1= require “scene1”

local scene = storyboard.newScene()

function scene:createScene( event )

function scene_time_bg()

    game.gameStart()

    btn_ball = display.newImage(“Photos/Ball.png”)

    btn_ball.x=900

    btn_ball.y=560

    btn_ball.xScale=1.1

    btn_ball.yScale=1.1

    btn_ball:toFront()

    score.setCombo()

end

function scene_time_start()

            local timeref=90

            ball.startBall(btn_ball)

            score.countDown()

            local timeLastEnemy = 0 

            local timeLastBomb = 0 

                local function dropBomb()

                    function dropbombAction()

                        crate.newBomb()

                    end

                timer.performWithDelay(math.random(600, 800), dropbombAction);

                end

                local function dropBox()

                    function dropcrateAction()

                        crate.newCrate()

                    end

                timer.performWithDelay(math.random(500, 900), dropcrateAction);

                end

                local function gameLoop(event)

                    if event.time - timeLastEnemy >= math.random(400, 800) then

                        dropBox()

                        timeLastEnemy = event.time

                    if event.time - timeLastBomb >= math.random(1500,2000) then

                        dropBomb()

                    timeLastBomb = event.time

                    end

                end

            end

local time = 60

ScoreRef=score.showScore()

Runtime:addEventListener(“enterFrame”, gameLoop)

      function removeEvent()

                 Runtime:removeEventListener(“enterFrame”, gameLoop)

      end

   end

end

function scene:enterScene( event )

scene_time_bg()

physics.start()

timer.performWithDelay(1000,scene_time_start)

end

function scene:exitScene( event )

storyboard.removeScene(“scene_time”)

end

function scene:destroyScene( event )

physics.stop()

removeEvent()

crate.removeTables()

end

scene:addEventListener( “createScene”, scene )

scene:addEventListener( “enterScene”, scene )

scene:addEventListener( “exitScene”, scene )

scene:addEventListener( “destroyScene”, scene )

return scene

Clearly I’m doing something wrong…Can you tell me what that is?

If you are starting physics in enterScene, you have to stop or pause physics in exitScene.

But it still doesn’t work the second time,  as I said… :X

Are you adding your objects to the main storyboard group (scene.view)?