i tried to add a collision listener to my spawned objects but i think i may be missing a step somewhere.
i don’t receive any errors but instead the objects just phase through each other and nothing happens.
i am also attempting to get the objects to only work together if they match.
for example. when object1.png spawns another object with a different image also named object1.png in a different folder spawns only these two objects should be able to perform a collision, but i cant play with that until i get the collision listener working in general.
here is my code
----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- local physics = require("physics") physics.start() physics.setGravity( 0, 0 ) physics.setDrawMode( "hybrid" ) math.randomseed(os.time()) local lives = 3 local score = 0 local died = false local ObjectsTable = {} local newObject local newObject2 local gameLoopTimer local livesText local ScoreText local delayRate = 3000 local fallRate = 100 local backGroup = display.newGroup() local mainGroup = display.newGroup() local uiGroup = display.newGroup() local backGround = display.newImageRect(backGroup, "Images/BackGround1.png", display.contentWidth, display.contentHeight) backGround.x = display.contentCenterX backGround.y = display.contentCenterY if system.getInfo( "androidApiLevel" ) and system.getInfo( "androidApiLevel" ) \< 19 then native.setProperty( "androidSystemUiVisibility", "lowProfile" ) else native.setProperty( "androidSystemUiVisibility", "immersiveSticky" ) end livesText = display.newText( uiGroup, "Lives: ".. lives, display.contentCenterX - 120,15, native.systemFont, 20 ) ScoreText = display.newText(uiGroup, "Score: ".. score,display.contentCenterX + 120, 15, native.systemFont, 20) local function updateText() livesText.text = "Lives: " .. lives ScoreText.text = "Score: " .. score end local function leftRight() local side = {140 , -140} return side[math.random(1, #side)] end -------------------------------------------------------------------------------- local function ObjectMatch() newObject2 = display.newImageRect(mainGroup,"Objects2/Object" .. randomNum ..".png", 69.2 , 60.8) newObject2.x = display.contentCenterX - leftRight() newObject2.y = display.contentCenterY + math.random ( -300, 300) physics.addBody( newObject2, "static", {radius=10 , isSensor=true}) newObject2.myName = "SubObject" .. randomNum end local function ObjectMatch2(randomNum) newObject3 = display.newImageRect(mainGroup,"Objects2/Object" .. randomNum .. ".png", 69.2 , 60.8) newObject3.x = display.contentCenterX + 140 newObject3.y = display.contentCenterY + math.random ( -300, 300) end -------------------------------------------------------------------------------- local function createObjects() randomNum = math.random(18) newObject = display.newImageRect(mainGroup,"Objects/Object" .. randomNum .. ".png", 69.2 , 60.8) table.insert( ObjectsTable, newObject ) physics.addBody( newObject, "dynamic", {radius = 10} ) newObject.myName = "Object" .. randomNum ObjectMatch(randomNum) --ObjectMatch2(randomNum) -------------------------------------------------------------------------------- local function dragObject ( event ) newObject = event.target local phase = event.phase if ( "began" == phase ) then display.currentStage:setFocus( newObject, event.id ) newObject.isFocus = true newObject.touchOffsetX = event.x - newObject.x newObject.touchOffsetY = event.y - newObject.y elseif ( newObject.isFocus ) then if ( "moved" == phase) then newObject.x = event.x - newObject.touchOffsetX newObject.y = event.y - newObject.touchOffsetY elseif ( "ended" == phase or "cancelled" == phase ) then display.currentStage:setFocus(newObject, nil ) newObject.isFocus = false end end return true end newObject:addEventListener( "touch", dragObject) -------------------------------------------------------------------------------- local whereFrom --from top newObject.y = -60 newObject.x = display.contentCenterX - math.random(-55, 60) -- keeps Objects within middle of screen newObject:setLinearVelocity( 0, fallRate) end local function gameLoop() createObjects() for i = #ObjectsTable, 1, -1 do local thisObject = ObjectsTable[i] if ( thisObject.x \< -100 or thisObject.x \> display.contentWidth + 100 or thisObject.y \< -100 or thisObject.y \> display.contentHeight + 100) then display.remove( thisObject ) table.remove( ObjectsTable, i ) end end end gameLoopTimer = timer.performWithDelay( delayRate, gameLoop, 0 ) -- param one sets delay between new Object spawn local function onCollsion( event ) if ( event.phase == "began") then local obj1 = event.object1 local obj2 = event.object2 if ((obj1.myname == "Object" and obj2.myName == "SubObject") or (obj1.myName == "SubObject" and obj2.myName == "Object")) then display.remove( obj1 ) display.remove( obj2 ) for i = #ObjectsTable, l, -l do if (ObjectsTable[i] == obj1 or ObjectsTable[i] == obj2) then table.remove ( ObjectsTable, i ) break end end score = score + 1 ScoreText.text = "Score: " .. score end end end Runtime:addEventListener( "collison", onCollsion)