Game.lua is duplicating my scene objects

There is a lot going on in the above code that needs to be corrected.

  1. You’re calling physics.start() twice. I don’t know if that will cause any problems specifically, but it’s best practice to only call it once.

  2. You need to declare your local “onCollision” function above when you assign it to your floor object.

  3. Your collision listener isn’t really going to be very useful in it’s current form. It’s checking the circle that has the identifier of “i”, but “i” keeps increasing and therefore might not be as accurate as you intended. You’re better off checking for an object type and removing the object in that way.

  4. Your spawn function isn’t great either. You should read over the blogpost on spawning and try to use the methods described there.

Good luck.

With Composer, you kind of need to call physics.start() twice.  First you have to call it in the create function and immediately pause it.  Start has to be called before you can do .addBody()  but you don’t want the simulation running while the scene is still off screen.  Then in the did phase of show() call it again to undo the physics.pause().  I know this sounds weird, but it works.

The onCollision function is declared locally after it’s used.  At the time you try to assign it, the value for onCollision is nil, and you’re not doing anything with them. 

Rob

Well, I learned something new there. I have a few projects with physics and Composer, and I only call .start() once. I better go back and see what’s going on there.

I mean feel free to have those crates start falling while the scene is still off screen :slight_smile:

Rob

local composer = require( "composer" ) local scene = composer.newScene() local \_W = display.contentWidth local \_H = display.contentHeight local physics = require("physics" ) physics.setScale( 30 ) physics.setGravity( 0, 0 ) physics.start( ) physics.setDrawMode( "hybrid" ) function scene:create( event ) local sceneGroup = self.view physics.start() ---------------------------- local BackGround = display.newImageRect("BackGround.png", 1080, 1920) return true end function scene:show(event) local sceneGroup = self.view if event.phase == "did" then ------------------------------------------- physics.start() -------------------------------------------- local floor = display.newImageRect("floor.png", 351, 32 ) floor.x = display.contentWidth \* 0.5 floor.y = 520 physics.addBody(floor, "static") ----------------------------------------------- local circle = {} local spawnCircles = function () local FallDown = math.random(display.contentWidth \* 0.2, display.contentWidth \* 0.8 ) circle = display.newImageRect("circle.png", 31, 31 ) circle.x = FallDown circle.y = -70 physics.addBody( circle, "dynamic", {density = .01, friction = 0.5, bounce = 0.1 }) circle.gravityScale = 0.1 physics.setGravity( 0, 10 ) end myTimer = timer.performWithDelay( 2000, spawnCircles, -1) --1000 is the time -------------------------------------------------------------- --------------------------------------------------------------- ------------------------------------------------------------------- end return true end -- ------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ------------------------------------------------------------------------------- return scene

Ok so i simplified the code and i still have the problem… any suggestions? 

oh that actually might be better !!

But i still want to fix this before i go changing code…

@Alex@Panc

@Rob Miracle

I found the problem… 

local floor = display.newImageRect("floor.png", 351, 32 ) floor.x = display.contentWidth \* 0.5 floor.y = 520 physics.addBody(floor, "static")

i guess its doing something with physics  or something 

Bump

If I remove the local floor code(image) then everything works… But if its there it starts glitching

Is there a reason you’re creating the floor in the show event instead of the create event?  I don’t see code for the collision handler.  How do you mean it’s glitching?

Give us something to help you with…

Also, please only bump your posts at most once per day.

Rob

Im not putting it in create scene because it doesnt make it a physics object for some reason…

and i bumped the post 3 days after no reply…

You bumped several times (didn’t say bump, but you bumped) on 5/29.

A scene using physics should either a) do a physics.start() followed by physics.pause() at the top of the module right after requiring physics or b) call physics.start() and immediately call physics.pause() in scene:create().   This enables adding bodies, but it doesn’t start the simulation.  Then in scene:show()'s did phase, call physics.start() to start up the simulation and then call physics.pause() again in scene:hide()'s will phase.

Rob 

tried that and it didnt work…

i think i have to do will and did and maybe that will help