Why are figures in the same place when scene is switched .

In my app I lose by a figure touching the ground . But when I lose and press the button that takes me back to level 1 the figures that previously touched the ground are in the same place  instead of starting in the  air . So basically my question is how do I get the scene to restart when i go back to it .

level1.lua:

local composer = require( "composer" ) local scene = composer.newScene() local physics = require( "physics" ) physics.start() physics.setGravity(0, 40) function scene:create( event ) local sceneGroup = self.view -- display background image local background = display.newImage( "background.png" ) background.x = 10; background.y = 308 -- display ground image local ground = display.newImage( "ground.png" ) ground.x = 145; ground.y = 480 ground.myName = "ground" ground.isGround = true physics.addBody( ground, "static" , { friction=0.5, bounce=0.1 } ) -- create object local myObject = display.newRect( 0, 0, 100, 30 ) myObject:setFillColor( 0 ) myObject.x = 145; myObject.y = 400 myObject.isMyObject = true physics.addBody( myObject, "kinematic" , { myObject, friction=0.5, bounce=2 } ) -- touch listener function function myObject:touch( event ) if event.phase == "moved" then self.x = event.x self.y = event.y end return true end -- make 'myObject' listen for touch events myObject:addEventListener( "touch", myObject ) local wall = display.newImageRect( "wall.png", 100, 10 ) wall.x, wall.y = -6, 387 wall.rotation = 90 physics.addBody( wall, "static" , { wall, friction=0.5, bounce=0.5 } ) local wall = display.newImageRect( "wall.png", 100, 10 ) wall.x, wall.y = -6, 300 wall.rotation = 90 physics.addBody( wall, "static" , { wall, friction=0.5, bounce=0.5 } ) local wall = display.newImageRect( "wall.png", 100, 10 ) wall.x, wall.y = -6, 202 wall.rotation = 90 physics.addBody( wall, "static" , { wall, friction=0.5, bounce=0.5 } ) local wall = display.newImageRect( "wall.png", 100, 10 ) wall.x, wall.y = -6, 104 wall.rotation = 90 physics.addBody( wall, "static" , { wall, friction=0.5, bounce=0.5 } ) local wall = display.newImageRect( "wall.png", 100, 10 ) wall.x, wall.y = -6, 7 wall.rotation = 90 physics.addBody( wall, "static" , { wall, friction=0.5, bounce=0.5 } ) local wall = display.newImageRect( "wall.png", 100, 10 ) wall.x, wall.y = 50, -49 physics.addBody( wall, "static" , { wall, friction=0.5, bounce=0.5 } ) local wall = display.newImageRect( "wall.png", 100, 10 ) wall.x, wall.y = 135, -49 physics.addBody( wall, "static" , { wall, friction=0.5, bounce=0.5 } ) local wall = display.newImageRect( "wall.png", 100, 10 ) wall.x = 232 wall.y = -49 physics.addBody( wall, "static" , { wall, friction=0.5, bounce=0.5 } ) local wall = display.newImageRect( "wall.png", 100, 10 ) wall.x = 335 wall.y = -49 physics.addBody( wall, "static" , { wall, friction=0.5, bounce=0.5 } ) local wall = display.newImageRect( "wall.png", 100, 10 ) wall.rotation = 90 wall.x = 325 wall.y = 3 physics.addBody( wall, "static" , { wall, friction=0.5, bounce=0.5 } ) local wall = display.newImageRect( "wall.png", 100, 10 ) wall.rotation = 90 wall.x = 325 wall.y = 98 physics.addBody( wall, "static" , { wall, friction=0.5, bounce=0.5 } ) local wall = display.newImageRect( "wall.png", 100, 10 ) wall.x = 325 wall.y = 195 wall.rotation = 90 physics.addBody( wall, "static" , { wall, friction=0.5, bounce=0.5 } ) local wall = display.newImageRect( "wall.png", 100, 10 ) wall.rotation = 90 wall.x = 325 wall.y = 292 physics.addBody( wall, "static" , { wall, friction=0.5, bounce=0.5 } ) local wall = display.newImageRect( "wall.png", 100, 10 ) wall.rotation = 90 wall.x = 325 wall.y = 387 physics.addBody( wall, "static" , { wall, friction=0.5, bounce=0.5 } ) local block = display.newImage( "block.png" ) block.rotation = 8 block.x = 200 block.myName = "block" block.isBlock = true physics.addBody( block, "dynamic" , { block, friction=0.5, bounce=0.5 } ) local function onCollision( self, event ) local other = event.other if( event.phase == "began" and self.isBlock and other.isGround ) then timer.performWithDelay( 30, function() composer.gotoScene ( "restart", "fade", 500 ); end) self:removeEventListener( "collision" ) end return true end block.collision = onCollision block:addEventListener( "collision" ) local block2 = display.newImage( "block2.png" ) block2.rotation = 8 block2.x = 100 block2.isBlock2 = true physics.addBody( block2, "dynamic" , { block2, friction=0.5, bounce=0.5 } ) local function onCollision( self, event ) local other = event.other if( event.phase == "began" and self.isBlock2 and other.isGround ) then timer.performWithDelay( 30, function() composer.gotoScene ( "restart", "fade", 500 ); end) self:removeEventListener( "collision" ) end return true end block2.collision = onCollision block2:addEventListener( "collision" ) sceneGroup:insert(background) sceneGroup:insert(ground) sceneGroup:insert(wall) sceneGroup:insert(block) sceneGroup:insert(block2) sceneGroup:insert(myObject) end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then elseif ( phase == "did" ) then end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then elseif ( phase == "did" ) then end end function scene:destroy( event ) local sceneGroup = self.view end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene

restart.lua:

local composer = require( "composer" ) local scene = composer.newScene() function scene:create( event ) local sceneGroup = self.view background = display.newImage( "background.png" ) background.x = 10; background.y = 308 ground = display.newImage( "ground.png" ) ground.x = 145; ground.y = 480 loseTxt = display.newImageRect( "loseTxt.png", 335, 200 ) loseTxt.x = 160 loseTxt.y = 30 button = display.newImageRect( "button.png", 110, 80 ) button.x = 160 button.y = 300 function button:touch(event) if event.phase == "ended" then composer.gotoScene("level1", "fade", 200) end return true end button:addEventListener("touch", button) sceneGroup:insert(background) sceneGroup:insert(ground) sceneGroup:insert(loseTxt) sceneGroup:insert(button) end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then elseif ( phase == "did" ) then end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then elseif ( phase == "did" ) then end end function scene:destroy( event ) local sceneGroup = self.view end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene

Thank you for your help! :slight_smile:

Wow, that’s a lot of code, and I’m not going to go through it all, but on a first pass…

It appears you are positioning your objects in scene:create, which is only executed one time… when the scene is created.  When you come back to the scene, scene:show is called, and it is in that function where you should position your objects.  

Hope this helps

–john

so should 1. switch all of the the display objects and positions to the show function or2. just the positioning for the objects cause I did 1 and it still doesn’t work .

For levels that I reload, I usually put all my object creation code in scene:create and place the positioning code elsewhere.  I guess it depends on how you are coding, but scene:show is an option.

I would not move everything to scene:show since ideally, you want to reuse the objects you already created… create them one time.  You don’t want to create the same objects every time you show your scene.

I usually have a “LevelStart” function where I position objects, turn on physics, etc.

I also have a “LevelEnd” function where I do things I need to do when the level ends.

–john

To expand a bit further… you can also take the “easy” way out and remove the game scene using removeScene inside of your restart.lua.  Then when the game scene is called again, scene:create will be called again and will reload and reposition your game objects.

I would do this as a last resort since you will be creating/destroying the same objects over and over, which may cause lag and memory issues (if you don’t fully remove your objects correctly).  Good practice would be to reuse the game objects you created one time, but I do understand that there may be times where that is not easily possible.

–john

You should position your objects in scene:show()'s “will” phase so they reposition before the scene transitions on the screen. Make sure to do a physics.pause() in scene:hide()'s “will” phase and then a physics.start() in the scene:show()'s “did” phase.

Wow, that’s a lot of code, and I’m not going to go through it all, but on a first pass…

It appears you are positioning your objects in scene:create, which is only executed one time… when the scene is created.  When you come back to the scene, scene:show is called, and it is in that function where you should position your objects.  

Hope this helps

–john

so should 1. switch all of the the display objects and positions to the show function or2. just the positioning for the objects cause I did 1 and it still doesn’t work .

For levels that I reload, I usually put all my object creation code in scene:create and place the positioning code elsewhere.  I guess it depends on how you are coding, but scene:show is an option.

I would not move everything to scene:show since ideally, you want to reuse the objects you already created… create them one time.  You don’t want to create the same objects every time you show your scene.

I usually have a “LevelStart” function where I position objects, turn on physics, etc.

I also have a “LevelEnd” function where I do things I need to do when the level ends.

–john

To expand a bit further… you can also take the “easy” way out and remove the game scene using removeScene inside of your restart.lua.  Then when the game scene is called again, scene:create will be called again and will reload and reposition your game objects.

I would do this as a last resort since you will be creating/destroying the same objects over and over, which may cause lag and memory issues (if you don’t fully remove your objects correctly).  Good practice would be to reuse the game objects you created one time, but I do understand that there may be times where that is not easily possible.

–john

You should position your objects in scene:show()'s “will” phase so they reposition before the scene transitions on the screen. Make sure to do a physics.pause() in scene:hide()'s “will” phase and then a physics.start() in the scene:show()'s “did” phase.