I’m making a simple endless runner type of game where the player has to dodge incoming obstacles. The spawning seems to work fine, everything seems to work fine…but I run into an issue when I had a enterframe listener to track the collision of the obstacles with the player. The game starts lagging a lot. I have a feeling this is due to some issue with not removing slots in the table where I’m containing the spawned obstacles. I feel like nothing I try seems to work, any help?
This is the event listener that seems to cause all of the problems (the game doesn’t lag unless this specific section is activated):
local function eventFramer(event) for i = 1, #spawnTable do if hasCollided( spawnTable[i], balloon ) then --branchTable[i]:removeSelf() --branchTable[i] = nil redRect:setFillColor(1,0,0) redRect.alpha = 0.7 transition.to(redRect,{time=300,alpha=0}) decreaseScore() end if hasCollided( spawnTable[i], topSide ) then print("branch removed") --spawnTable.remove(spawnTable[i]) spawnTable[i]:removeSelf() spawnTable[i] = nil end end return true end
I’m spawning the obstacles continuously using the exact method given in this guide: https://coronalabs.com/blog/2011/09/14/how-to-spawn-objects-the-right-way/.
--Function to spawn an object local function spawn(params) local object = display.newImage(params.image) --Set the objects table to a table passed in by parameters object.objTable = params.objTable --Automatically set the table index to be inserted into the next available table index object.index = #object.objTable + 1 --Give the object a custom name object.myName = "Object : " .. object.index --If the object should have a body create it, else dont. if params.hasBody then --Allow physics parameters to be passed by parameters: object.density = params.density or 0 object.friction = params.friction or 0 object.bounce = params.bounce or 0 object.isSensor = params.isSensor or false object.bodyType = params.bodyType or "dynamic" physics.addBody(object, object.bodyType, {density = object.density, friction = object.friction, bounce = object.bounce, isSensor = object.isSensor}) end --The objects group object.group = params.group or nil --If the function call has a parameter named group then insert it into the specified group object.group:insert(object) --Insert the object into the table at the specified index object.objTable[object.index] = object return object end --Create a table to hold our spawns local spawnTable = {}
And I’m using the collision detection method described in this guide: http://omnigeek.robmiracle.com/2011/12/14/collision-detection-without-physics/
local function hasCollided( obj1, obj2 ) if ( obj1 == nil ) then --make sure the first object exists return false end if ( obj2 == nil ) then --make sure the other object exists return false end local left = obj1.contentBounds.xMin \<= obj2.contentBounds.xMin and obj1.contentBounds.xMax \>= obj2.contentBounds.xMin local right = obj1.contentBounds.xMin \>= obj2.contentBounds.xMin and obj1.contentBounds.xMin \<= obj2.contentBounds.xMax local up = obj1.contentBounds.yMin \<= obj2.contentBounds.yMin and obj1.contentBounds.yMax \>= obj2.contentBounds.yMin local down = obj1.contentBounds.yMin \>= obj2.contentBounds.yMin and obj1.contentBounds.yMin \<= obj2.contentBounds.yMax return (left or right) and (up or down) end
Am I just removing objects from the table incorrectly or what?
Thank you.