Bouncing on air?

I just recently had a problem with the scene not reloading but with some help it is kinda fixed . The blocks start off in the correct spot but when they come down they appear to be bouncing on nothing .

Level1.lua:

local composer = require( "composer" ) local scene = composer.newScene() local physics = require( "physics" ) physics.start() physics.setGravity(0, 40) local disposeGroup local onBack function scene:create( event ) local sceneGroup = self.view end function scene:willEnter( event ) local sceneGroup = self.view disposeGroup = display.newGroup() sceneGroup:insert( disposeGroup ) background = display.newImage( "background.png" ) background.x = 10; background.y = 308 sceneGroup:insert(background) ground = display.newImage( "ground.png" ) ground.x = 145; ground.y = 480 sceneGroup:insert(ground) ground.myName = "ground" ground.isGround = true physics.addBody( ground, "static" , { friction=0.5, bounce=0.1 } ) myObject = display.newRect( 0, 0, 100, 30 ) myObject.x = 145; myObject.y = 400 sceneGroup:insert(myObject) myObject:setFillColor( 0 ) myObject.isMyObject = true physics.addBody( myObject, "kinematic" , { myObject, friction=0.5, bounce=2 } ) function myObject:touch( event ) if event.phase == "moved" then self.x = event.x self.y = event.y end return true end myObject:addEventListener( "touch", myObject ) wall = display.newImageRect( "wall.png", 600, 300 ) wall.x = -150 wall.y = 250 sceneGroup:insert(wall) wall.rotation = 90 physics.addBody( wall, "static" , { wall, friction=0.5, bounce=0.5 } ) wall2 = display.newImageRect( "wall2.png", 500, 300 ) sceneGroup:insert(wall2) wall2.x = 210 wall2.y = -194 wall.rotation = 90 physics.addBody( wall2, "static" , { wall2, friction=0.5, bounce=0.5 } ) wall3 = display.newImageRect( "wall3.png", 600, 300 ) sceneGroup:insert(wall3) wall3.x = 470 wall3.y = 250 wall3.rotation = 90 physics.addBody( wall3, "static" , { wall3, friction=0.5, bounce=0.5 } ) block = display.newImage( "block.png" ) sceneGroup:insert(block) block.x = 200 block.rotation = 8 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", 0 ); end) self:removeEventListener( "collision" ) end return true end block.collision = onCollision block:addEventListener( "collision" ) block2 = display.newImage( "block2.png" ) sceneGroup:insert(block2) block2.x = 100 block2.rotation = 8 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", 0 ); end) self:removeEventListener( "collision" ) end return true end block2.collision = onCollision block2:addEventListener( "collision" ) end function scene:didEnter( event ) local sceneGroup = self.view end function scene:willExit( event ) local sceneGroup = self.view end function scene:didExit( event ) local sceneGroup = self.view display.remove( disposeGroup ) disposeGroup = nil end function scene:show( event ) sceneGroup = self.view local willDid = event.phase if( willDid == "will" ) then self:willEnter( event ) elseif( willDid == "did" ) then self:didEnter( event ) end end function scene:hide( event ) sceneGroup = self.view local willDid = event.phase if( willDid == "will" ) then self:willExit( event ) elseif( willDid == "did" ) then self:didExit( event ) end end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) --------------------------------------------------------------------------------- return scene

What is the problem ? And note that nothing has transparent sides . Why does it appear to bounce on air ?

Thank you kind sir ^_^ .

Turn on hybrid draw mode to see what is going on:

physics.setDrawMode(“hybrid”)

I did it and the objects in my scene don’t go away in reastart.lua . How do  make them go away in restart.lua ?

Are you saying, you turned on hybrid mode and saw that objects were left behind? i.e. Where you thought there was nothing there were actually bodies?

Be sure you’re inserting your all objects into the dispose group and destroying that group when you leave.

Hi @trueisawesome,

As I glance at your code, I see that you’re mixing both Storyboard and Composer into everything. I see both Storyboard event handlers and Composer event handlers, and the first step would be to clean that all up (use Composer and not Storyboard).

Brent

@Brent,

I think it looks like storyboard because he is using code from my composer framework that splits did/will show and did/will hide sub-events into separate distinct methods following the old (storyboard) naming convention:

function scene:show( event ) sceneGroup = self.view local willDid = event.phase if( willDid == "will" ) then self:willEnter( event ) elseif( willDid == "did" ) then self:didEnter( event ) end end function scene:hide( event ) sceneGroup = self.view local willDid = event.phase if( willDid == "will" ) then self:willExit( event ) elseif( willDid == "did" ) then self:didExit( event ) end end

I wrote and provided this framework variant because I preferred the old split and it made things easier for folks transitioning over to composer.

Ok

@roaminggamer

       The code you provided me ( thank you for that ) has a reload button within the scene that remakes the scene . I have a restart.lua file with my button that takes me back to the actual game file . I have added the localizations but how do I make it so that 1 button in one scene takes me to a different one BUT also remakes it like the button in the playGUI.lua file ?

Tell me if this is confusing I m bad a wording things

Thank you for your much needed help .

I’m a little unclear on what you’re saying, but if you’re asking how to add more buttons and scenes…  You’ll need to learn this on your own, as follows:

  1. Take the blank template I suggested you use, and add more buttons, listeners, and scenes to see how the process works without your own code in the mix.

  2. Apply this learning to your current project.

I know i can be bad a t wording things so i see how you got confused. My apologies.
What im trying to say is:

You know how in the code you provided me there is a playGui. Lua . In that lua file there is a button to reload the scene with everything reset . But the reload button in playGui. Lua is basically like the restart button in my game. The difference is i have a restart scene with my restart button within. So if i put something similar to your code it will just reload the scene im in ( restart. Lua ) and not go to level1.lua . I want to know how to make my button in restart. Lua take me to the level1 scene and reset everything with none of the errors i had before.
Thank you.

Ah, I see.  While you can accomplish what you want to do, it can’t be done by reloading the same scene that is in memory.

Instead of trying to exit the scene and come back or to simply ‘load it again’, do this:

  1. (build function) Separate the code to build your scene into a function that takes a single argument (the sceneGroup).

  2. In didEnter() call that function.

  3. (destroy function) Separate the code that is used to destroy disposeGroup into a separate function.

  4. In didExit() call that function.

  5. When the user touches your ‘restart’ button, call the destroy function and then the build function.

Turn on hybrid draw mode to see what is going on:

physics.setDrawMode(“hybrid”)

I did it and the objects in my scene don’t go away in reastart.lua . How do  make them go away in restart.lua ?

Are you saying, you turned on hybrid mode and saw that objects were left behind? i.e. Where you thought there was nothing there were actually bodies?

Be sure you’re inserting your all objects into the dispose group and destroying that group when you leave.

Hi @trueisawesome,

As I glance at your code, I see that you’re mixing both Storyboard and Composer into everything. I see both Storyboard event handlers and Composer event handlers, and the first step would be to clean that all up (use Composer and not Storyboard).

Brent

@Brent,

I think it looks like storyboard because he is using code from my composer framework that splits did/will show and did/will hide sub-events into separate distinct methods following the old (storyboard) naming convention:

function scene:show( event ) sceneGroup = self.view local willDid = event.phase if( willDid == "will" ) then self:willEnter( event ) elseif( willDid == "did" ) then self:didEnter( event ) end end function scene:hide( event ) sceneGroup = self.view local willDid = event.phase if( willDid == "will" ) then self:willExit( event ) elseif( willDid == "did" ) then self:didExit( event ) end end

I wrote and provided this framework variant because I preferred the old split and it made things easier for folks transitioning over to composer.

Ok

@roaminggamer

       The code you provided me ( thank you for that ) has a reload button within the scene that remakes the scene . I have a restart.lua file with my button that takes me back to the actual game file . I have added the localizations but how do I make it so that 1 button in one scene takes me to a different one BUT also remakes it like the button in the playGUI.lua file ?

Tell me if this is confusing I m bad a wording things

Thank you for your much needed help .

I’m a little unclear on what you’re saying, but if you’re asking how to add more buttons and scenes…  You’ll need to learn this on your own, as follows:

  1. Take the blank template I suggested you use, and add more buttons, listeners, and scenes to see how the process works without your own code in the mix.

  2. Apply this learning to your current project.

I know i can be bad a t wording things so i see how you got confused. My apologies.
What im trying to say is:

You know how in the code you provided me there is a playGui. Lua . In that lua file there is a button to reload the scene with everything reset . But the reload button in playGui. Lua is basically like the restart button in my game. The difference is i have a restart scene with my restart button within. So if i put something similar to your code it will just reload the scene im in ( restart. Lua ) and not go to level1.lua . I want to know how to make my button in restart. Lua take me to the level1 scene and reset everything with none of the errors i had before.
Thank you.

Ah, I see.  While you can accomplish what you want to do, it can’t be done by reloading the same scene that is in memory.

Instead of trying to exit the scene and come back or to simply ‘load it again’, do this:

  1. (build function) Separate the code to build your scene into a function that takes a single argument (the sceneGroup).

  2. In didEnter() call that function.

  3. (destroy function) Separate the code that is used to destroy disposeGroup into a separate function.

  4. In didExit() call that function.

  5. When the user touches your ‘restart’ button, call the destroy function and then the build function.