Hi, I’m trying to use collision detection in my simple game where there are circles that follow a circular path. I tried using just the basic collision detection example shown on the site but cant seem to get it to work, the collision between the two balls dosen’t seem to be detected at all. The code in question is below. Thanks in advance!
--Localise the maths vars. local mPi = math.pi local mS = math.sin local mC = math.cos local composer = require( "composer" ) local scene = composer.newScene() local physics = require "physics" physics.start(); physics.setGravity( 0, 0 ) physics.pause() gameOver = false local Out = true local Enemy1Out = true local Enemy2Out = false local Enemy3Out = true local Enemy4Out = true local Enemy1 local Enemy2 local Enemy3 local Enemy4 physics.setDrawMode( "hybrid" ) local EnemyCount local screenW, screenH, halfW, halfH = display.contentWidth, display.contentHeight, display.contentWidth\*0.5,display.contentWidth\*0.5 function scene:create( event ) -- Called when the scene's view does not exist. -- -- INSERT code here to initialize the scene -- e.g. add display objects to 'sceneGroup', add touch listeners, etc. local sceneGroup = self.view end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then -- Called when the scene is still off screen and is about to move on screen elseif phase == "did" then -- Called when the scene is now on screen -- -- INSERT code here to make the scene come alive -- e.g. start timers, begin animation, play audio, etc. timer.performWithDelay(5000, spawnBallOne, 1 ) spawnTmr2 = timer.performWithDelay(15000, spawnBallTwo, 1 ) spawnTmr3 = timer.performWithDelay(35000, spawnBallThree, 1 ) end end scene:addEventListener( "show", scene ) scene:addEventListener( "create", scene ) local function makeCircle( startPointXY, endPointXY, degreesStart, degreesEnd, yScale, xScale, value) local xy = {} --Holds the points we make. local distance = endPointXY[1] - startPointXY[1] --The radius --Sets the scales if needed. if yScale == nil then yScale = 1 end if xScale == nil then xScale = 1 end if value == nil then value = 2 end --Changes the direction of the path if needed. if degreesEnd \< degreesStart then value = -value end --Now create the circlular path! local i for i=degreesStart, degreesEnd, value do local radians = i\*(mPi/180) local x = (endPointXY[1] + ( distance \* mS( radians ) )\*xScale) local y = (endPointXY[2] + ( distance \* mC( radians ) )\*yScale) xy[#xy+1] = {x, y} --Display a circle to show you the path your making. local circler = display.newCircle(0,0,1) circler.x = x; circler.y = y; circler:setFillColor(255,60,60) end --Return the array. return xy end local function touch( event ) if ( event.phase == "began" ) then if Out == true then Out = false elseif Out == false then Out = true end elseif ( event.phase == "moved" ) then elseif ( event.phase == "ended" ) then end return true --prevents touch propagation to underlying objects end function onLocalCollision( self, event ) if ( event.phase == "began" ) then print( self.myName .. ": collision began with " .. event.other.myName ) gameOver = true print("HWWFBAIBJDIABWDIBWLIAFIZwnilfbeiualnfljeanofieneaopjepof") elseif ( event.phase == "ended" ) then print( self.myName .. ": collision ended with " .. event.other.myName ) end end local function spawnBallOne() --local ballType = math.random(3) --if ballType == 1 then waypointsEnemy1 = makeCircle( {80,240}, {160,240}, 0, 360, 1.25 , 1.25 ) Enemy1 = display.newCircle(0,0,12) --Automatically set the table index to be inserted into the next available table index physics.addBody( Enemy1, "kinematic" ,{ density=1.0, friction=0.3, bounce=0.3 } ) Enemy1.isAwake = true Enemy1.isSleepingAllowed = false Enemy1.collision = onLocalCollision Enemy1:addEventListener("collision", Enemy1) end --[[elseif ballType == 2 then waypointsEnemy1 = makeCircle( {80,240}, {160,240}, 0, 360, 1.25 , 1.25 ) Enemy1 = display.newCircle(0,0,10) --Automatically set the table index to be inserted into the next available table index physics.addBody( Enemy1, "kinematic" ,{ density=1.0, friction=0.3, bounce=0.3 } ) Enemy1.collision = onLocalCollision Enemy1:addEventListener("collision", Enemy1) elseif ballType == 3 then waypointsEnemy1 = makeCircle( {80,240}, {160,240}, 0, 360, 1.25 , 1.25 ) Enemy1 = display.newCircle(0,0,14) --Automatically set the table index to be inserted into the next available table index physics.addBody( Enemy1, "kinematic" ,{ density=1.0, friction=0.3, bounce=0.3 } ) Enemy1.collision = onLocalCollision Enemy1:addEventListener("collision", Enemy1) end]]-- local function spawnBallTwo() --local ballType = math.random(3) --if ballType == 1 then Enemy2 = display.newCircle(0,0,12) --Automatically set the table index to be inserted into the next available table index physics.addBody( Enemy2, "kinematic" ,{ density=1.0, friction=0.3, bounce=0.3 } ) Enemy2.isAwake = true Enemy2.isSleepingAllowed = false Enemy2.collision = onLocalCollision Enemy2:addEventListener("collision", Enemy2) end local function spawnBallThree() local ballType = math.random(2) if ballType == 1 then waypointsEnemy3 = makeCircle( {80,240}, {160,240}, 0, 360, 1.25 , 1.25 ) elseif ballType == 2 then waypointsEnemy3 = makeCircle( {97,240}, {160,240}, 0, 360,1.15,1.15) end Enemy3 = display.newCircle(0,0,12) --Automatically set the table index to be inserted into the next available table index physics.addBody( Enemy3, "kinematic" ,{ density=1.0, friction=0.3, bounce=0.3 } ) Enemy3.isAwake = true Enemy3.isSleepingAllowed = false Enemy3.collision = onLocalCollision Enemy3:addEventListener("collision", Enemy3) end local function spawnBallFour() local ballType = math.random(2) if ballType == 1 then waypointsEnemy4 = makeCircle( {80,240}, {160,240}, 0, 360, 1.25 , 1.25 ) elseif ballType == 2 then waypointsEnemy4 = makeCircle( {97,240}, {160,240}, 0, 360,1.15,1.15) end Enemy4 = display.newCircle(0,0,12) --Automatically set the table index to be inserted into the next available table index physics.addBody( Enemy4, "kinematic" ,{ density=1.0, friction=0.3, bounce=0.3 } ) Enemy4.isAwake = true Enemy4.isSleepingAllowed = false Enemy4.collision = onLocalCollision Enemy4:addEventListener("collision", Enemy3) end --Plug in the needed variables. --You could also do the yScale, xScale and value but that doesnt need to be put in. timer.performWithDelay(10, spawnBallOne, 1 ) timer.performWithDelay(5000, spawnBallTwo, 1 ) timer.performWithDelay(15000, spawnBallThree, 1 ) timer.performWithDelay(30000, spawnBallFour,1 ) background = display.newImage("background.png") background:translate(160,240) background:addEventListener( "touch", touch ) waypointsEnemy2 = makeCircle( {97,240}, {160,240}, 0, 360,1.15,1.15) local waypointsOut = makeCircle( {80,240}, {160,240}, 360, 0,1.25,1.25) local waypointsIn = makeCircle( {97,240}, {160,240}, 360, 0,1.15,1.15) --local waypointsEnemyOut = makeCircle( {80,300}, {160,300}, 0, 360,1.25,1.25) --local waypointsEnemyIn = makeCircle( {97,300}, {160,300}, 0, 360,1.15,1.15) local circlePath = display.newCircle(160, 240, 86 ) circlePath.strokeWidth = 5 circlePath:setFillColor(255,255,255,0) --local circlePathEnemy = display.newCircle(160, 300, 86 ) --circlePathEnemy.strokeWidth = 5 --circlePathEnemy:setFillColor(255,255,255,0) --Now we are going to move a circle around that path..... --FIrst create the circle circle = display.newCircle(0,0,12) physics.addBody(circle,"dynamic",{ density=1.0, friction=0.3, bounce=0.3 } ) ScoreAmount = 0 Score = display.newText(ScoreAmount,160,240,native.systemFont,48) circle.isSensor = true circle.isAwake = true circle.isSleepingAllowed = false circle.collision = onLocalCollision circle:addEventListener("collision", circle) --Then create the gameLoop function which will run every frame. local int = 1 local int1 = 1 local int2 = math.random(50) local int3 = math.random(100) local int4 = math.random(50) local function gameLoop() if Out == true then if int \<= #waypointsOut then circle.x = waypointsOut[int][1] circle.y = waypointsOut[int][2] int = int + 1 else int = 1 ScoreAmount = ScoreAmount + 1 Score.text = ScoreAmount end elseif Out == false then if int \<= #waypointsOut then circle.x = waypointsIn[int][1] circle.y = waypointsIn[int][2] int = int + 1 else int = 1 ScoreAmount = ScoreAmount + 1 Score.text = ScoreAmount end end if Enemy1 ~= nil and gameOver == false then if int1 \<= #waypointsEnemy1 then Enemy1.x = waypointsEnemy1[int1][1] Enemy1.y = waypointsEnemy1[int1][2] int1 = int1 + 1 else int1 = 1 end elseif gameOver == true then Enemy1.x = waypointsEnemy1[int1][1] Enemy1.y = waypointsEnemy1[int1][2] end if Enemy2 ~= nil and gameOver == false then if int2 \<= #waypointsEnemy2 then Enemy2.x = waypointsEnemy2[int2][1] Enemy2.y = waypointsEnemy2[int2][2] int2 = int2 + 1 else int2 = 1 end elseif gameOver == true then Enemy2.x = waypointsEnemy2[int2][1] Enemy2.y = waypointsEnemy2[int2][2] end if Enemy3 ~= nil and gameOver == false then if int3 \<= #waypointsEnemy3 then Enemy3.x = waypointsEnemy3[int3][1] Enemy3.y = waypointsEnemy3[int3][2] int3 = int3 + 1 else int3 = 1 end elseif gameOver == true then Enemy3.x = waypointsEnemy3[int3][1] Enemy3.y = waypointsEnemy3[int3][2] end if Enemy4 ~= nil and gameOver == false then if int4 \<= #waypointsEnemy4 then Enemy4.x = waypointsEnemy4[int4][1] Enemy4.y = waypointsEnemy4[int4][2] int4 = int4 + 1 else int4 = 1 end elseif gameOver == true then Enemy4.x = waypointsEnemy4[int4][1] Enemy4.y = waypointsEnemy4[int4][2] end end function scene:destroy( event ) -- Called prior to the removal of scene's "view" (sceneGroup) -- -- INSERT code here to cleanup the scene -- e.g. remove display objects, remove touch listeners, save state, etc. local sceneGroup = self.view package.loaded[physics] = nil physics = nil end scene:addEventListener( "destroy", scene ) Runtime:addEventListener("enterFrame", gameLoop)