Hi, I have a problem with removing physics bodies (petals) that fall from the top of the screen to the bottom with gravity. The petals are falling constantly on the title screen and because they aren’t being removed until the scene is changed the memory use just keeps going up until scene change. I have set up a sensor off the bottom of the page and can get a collision to fire with the petal objects but then the simulator crashes with the error “Attempt to index global ‘Petal’ (a nil value)”. Based on this I assume it means I am not addressing the Petal object correctly in the collision but I am not sure how to do this as the petals are randomly new to this and I am still learning how to code…
The code that creates the petals is (this works on its own):
-- Petal positioning layer.Petal = {} layer.gp\_Petal = display.newGroup() layer.c\_Petal = 0 sceneGroup:insert( layer.gp\_Petal) -- Multipliers for Petal local Petal\_m\_loop = 1 --1 plays multiplier forever local Petal\_m\_counter = 100; composer.Petal\_m\_restart = 100 composer.mt\_Petal = function(counter) physics.setGravity(math.random(9\*-1,9)/10, 4); layer.Petal[counter] = display.newImageRect( composer.imgDir.. "p13\_petal.png", 62, 50 ); layer.Petal[counter].x = math.random(0,2048) layer.Petal[counter].y = math.random(-50,-50) layer.Petal[counter].alpha = math.random(100,100) / 100 layer.Petal[counter].oldAlpha = 1 layer.Petal[counter].xScale = math.random(60,100) / 100 layer.Petal[counter].yScale = layer.Petal[counter].xScale layer.Petal[counter].rotation = math.random(0,360) layer.Petal[counter].gravityScale = 0.15 local pweight = math.random(1, 2); local physicsData = (require "p13\_fairies\_physics").physicsData(1.0); physics.addBody(layer.Petal[counter], "dynamic", physicsData:get("p13\_petal")); local a = pweight \* layer.Petal[counter].xScale; layer.Petal[counter].linearDamping = a; layer.Petal[counter].angularDamping = 0.8 layer.Petal[counter].myName = "Petal" layer.gp\_Petal:insert(layer.Petal[counter]) end local function ct\_Petal() layer.c\_Petal = layer.c\_Petal + 1 if composer.mt\_Petal ~= nil then composer.mt\_Petal( layer.c\_Petal) end if (layer.c\_Petal == composer.Petal\_m\_restart and Petal\_m\_loop == 1) then composer.timerStash.mt = timer.performWithDelay( 400, ct\_Petal, 100 ) composer.Petal\_m\_restart = layer.c\_Petal + Petal\_m\_counter end end composer.multi\_Petal = function() composer.timerStash.mt = timer.performWithDelay( 400, ct\_Petal, 100 ) end composer.multi\_Petal()
and the code that sets up the sensor and attempts to destroy petals when they fall off screen which crashes the simulator is:
-- petal\_floor positioning layer.petal\_floor = display.newRect( 1024, 1600, 2048, 20 ) layer.petal\_floor.oriX = layer.petal\_floor.x; layer.petal\_floor.oriY = layer.petal\_floor.y layer.petal\_floor.oriXs = layer.petal\_floor.xScale; layer.petal\_floor.oriYs = layer.petal\_floor.yScale layer.petal\_floor:setFillColor (0, 0, 0) layer.petal\_floor.alpha = 1; layer.petal\_floor.oldAlpha = 1 sceneGroup:insert( 1, layer.petal\_floor); sceneGroup.petal\_floor = layer.petal\_floor physics.addBody(layer.petal\_floor, "static", { isSensor=true } ) layer.petal\_floor.myName = "petal\_floor" local function onCollision\_petal\_floor(self, event) if event.phase == "began" and event.other.myName == "Petal" then Petal:removeSelf() Petal = nil end end layer.petal\_floor.collision = onCollision\_petal\_floor layer.petal\_floor:addEventListener("collision", petal\_floor)
Appreciate any help anyone can give me!