Hi. I read through the code and there are a number of issues (not putting you down here OK, just listing them)
- Thanks for posting the code in a code block but it needed a little more cleaning to make it legible. However, I grabbed it and fixed it.
- You don’t want to use an endless timer (see my variation below). They seem like a good idea, but often lead to issues if you make a small mistake. Better to call timer again from the function.
- You ignored the fact that show() is called twice (once for phase == will, again for phase == did).
- You almost never want to define a function body inside another function body, especially not in show() or create() ,etc. When using composer declare the functions outside the scene functions. If you must declare them at the top of your file and define the below, or just define them before the scene functions (see my sample below).
- Others… can’t remember now.
Anyways, here is a working example ( I changed some code since I don’t have all of yours and don’t have your art assets):
local composer = require( "composer" ) local physics = require("physics" ) local scene = composer.newScene() local \_W = display.contentWidth local \_H = display.contentHeight local cw = display.contentWidth \* 0.2 local ch = display.contentWidth \* 0.8 local function onCollision( self, event ) return true end local maxCircles = 5 local circles = {} local function spawnCircles( group ) if( #circles \>= maxCircles) then return end --local circle = display.newImageRect( group, "circles.png", 31, 31 ) local circle = display.newCircle( group, 31, 31, 10 ) circles[#circles+1] = circle circle.x = math.random( cw, ch ) circle.y = -70 physics.addBody( circle, "dynamic", { density = .01, friction = 0.5, bounce = 0.1 }) circle.gravityScale = 0.1 timer.performWithDelay( 2000, function() spawnCircles( group ) end ) circle.collision = onCollision circle:addEventListener( "collision" ) end function scene:create( event ) local sceneGroup = self.view physics.setScale( 30 ) physics.setGravity( 0, 10 ) physics.start( ) physics.setDrawMode( "hybrid" ) end function scene:show( event ) local sceneGroup = self.view local willDid = event.phase if( willDid == "will" ) then elseif( willDid == "did" ) then timer.performWithDelay( 2000, function() spawnCircles( sceneGroup ) end ) end end -- ------------------------------------------------------------------------------- scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ------------------------------------------------------------------------------- return scene