Completely reinitialize scene

Hi all,

This is kinda two part question both interrelated.

I have a scene which has an object which is a newImageRect(), the scene has gravity and I set the gravity to 0 by physics.setGravity( 0, 0 ). I also call physics.addBody(myObject,“dynamic”,{bounce=0, friction=0.2, radius=98}) so I can interact with the myObject. The first time I load the scene the object appears in the correct Y and X coordinate, however when I move out out of the scene and back to it the object moves a little up or down and stops but doesn’t appear like the first time the scene starts in the correct Y position. 

My question is kinda how can I move out of a scene and go back to it as though I am going to that scene for the first time - the text objects I have are fine but I think it has to do with the physics and the object is dynamic in that it appears to drop or go up temporarily before it stops when going back to the scene again. 

I really cant understand this??

The dynamic object is this code in the create()

myObject= display.newImageRect("images/myObject.png",200,200) myObject.x = display.contentCenterX myObject.y = display.contentCenterY physics.addBody(myObject,"dynamic",{bounce=0, friction=0.2, radius=98}) myObject.angularDamping = 1 myObject:applyTorque(4) sceneGroup:insert(myObject) physics.setGravity( 0, 0 )

I just want everything to appear in the correct place including the dynamic myObject. In the BackButton override do I need to do something further in there to ‘reset everthing’

My current backbutton override looks like this:

-- Called when a key event has been received local function onKeyEvent( event ) -- If the "back" key was pressed on Android or Windows Phone, make sure it gets back to menu if ( event.keyName == "back" ) then local platformName = system.getInfo( "platformName" ) if ( platformName == "Android" ) or ( platformName == "WinPhone" ) then composer.removeScene("scenes.total\_distance\_game\_default" ) composer.gotoScene("scenes.menu") end end return true end Runtime:addEventListener( "key", onKeyEvent )

Think I got this working now calling in my backbutton override:

physics.removeBody(myObject)

myObject.gravityScale = 0

When you leave a scene you have the opportunity to put things in the correct place in the scene:hide will or did.

I think all you need to understand is here composer.gotoScene()

What I would do is in your back button code do:

local function onKeyEvent( event ) -- If the "back" key was pressed on Android or Windows Phone, make sure it gets back to menu if ( event.keyName == "back" and event.phase == "up") then local platformName = system.getInfo( "platformName" ) if ( platformName == "Android" ) or ( platformName == "WinPhone" ) then composer.gotoScene("scenes.menu") end end return true end

Then in your total_distance_game_default.lua file:

function scene:hide( event ) if event.phase == "did" then composer.removeScene("scenes.total\_distance\_game\_default") end end

Rob

If you are coming back to the scene, you could recycle it too. 

composer.removeScene("scenes.total\_distance\_game\_default", true)

The shouldRecyle option determines if the scene module should be un-required or not. Many developers execute code in the main chunk that’s important when loading the scene, creating it and showing it. If you recycle the scene, it’s not un-required and this could lead to the scene not working as the developer expects. Of course, the solution is to not put executable code in the main chunk, but that’s can make building the scene a bit more challenging too.

Rob

Think I got this working now calling in my backbutton override:

physics.removeBody(myObject)

myObject.gravityScale = 0

When you leave a scene you have the opportunity to put things in the correct place in the scene:hide will or did.

I think all you need to understand is here composer.gotoScene()

What I would do is in your back button code do:

local function onKeyEvent( event ) -- If the "back" key was pressed on Android or Windows Phone, make sure it gets back to menu if ( event.keyName == "back" and event.phase == "up") then local platformName = system.getInfo( "platformName" ) if ( platformName == "Android" ) or ( platformName == "WinPhone" ) then composer.gotoScene("scenes.menu") end end return true end

Then in your total_distance_game_default.lua file:

function scene:hide( event ) if event.phase == "did" then composer.removeScene("scenes.total\_distance\_game\_default") end end

Rob

If you are coming back to the scene, you could recycle it too. 

composer.removeScene("scenes.total\_distance\_game\_default", true)

The shouldRecyle option determines if the scene module should be un-required or not. Many developers execute code in the main chunk that’s important when loading the scene, creating it and showing it. If you recycle the scene, it’s not un-required and this could lead to the scene not working as the developer expects. Of course, the solution is to not put executable code in the main chunk, but that’s can make building the scene a bit more challenging too.

Rob