I have a function that spawns an object (a cow) every 9.2 seconds. It works perfectly fine. I also have two collision methods (one bassed on physics and one is a non physics).
The randomly spawned cows should colide with another rectangular object (a beam).
The problem is that everything works fine (aparently),but when the beam colides with the cow,nothing happens.Here is the code.It shows up no errror.It simply doesn’t work.
The weird thing is that the collision system (both of them) works perfect with the cow,when the spawning function is deleted.
Thank you for your help!
-- this is the physics collision system. local function cow (event) sheetData = {width = 100,height = 100,numFrames = 14,sheetContentWidth = 200,sheetContentHeight = 700} mySheet = graphics.newImageSheet ("cow.png",sheetData) sequenceData = {{name = "cowOne2",start = 1,count = 8,time = 1000,loopcount = 0}, {name = "cowBeaming",frames = {9,10,11,12,13,14},time = 1000,loopcount = 0}} cowOne = display.newSprite (mySheet,sequenceData) cowOne.x = 35 cowOne.y = 450 cowOne.rotation = 90 cowOne:play() physics.addBody (cowOne,"static",{friction = 0.2,radius = 22}) cowOne.gravityScale = 0 cowOne.isSensor = true cowOne:setLinearVelocity (0,0) return cowOne end local function cowSpawnOne (event) for i = 0,0 do local c1 = cow() c1.x = 38 c1.y = math.random (600,850) local removeCow = function () display.remove (c1) c1 = nil end cowIntro = transition.to (c1,{time = 23000,y = -50,onComplete = removeCow}) if score == 300 then cowIntro = transition.to (c1,{time = 18000,y = -50,onComplete = removeCow}) if score \>= 300 then cowIntro = transition.to (c1,{time = 15000,y = -50,onComplete = removeCow}) end end end end local function cowCollision (event) if event.phase == "began" then cowOne:setSequence ("cowBeaming") cowOne:play() end end -- here are the events listeners: timer.performWithDelay (9200,cowSpawnOne,0) cowOne:addEventListener("collision",cowCollision)
--The non-physics collision system local function cow(event) local sheetData = {width = 100,height = 100,numFrames = 14,sheetContentWidth = 200,sheetContentHeight = 700} local mySheet = graphics.newImageSheet ("cow.png",sheetData) local sequenceData = {{name = "cowOne2",start = 1,count = 8,time = 1000,loopcount = 0}, {name = "cowBeaming",frames = {9,10,11,12,13,14},time = 1000,loopcount = 1}} local cowOne = display.newSprite (mySheet,sequenceData) cowOne.x = 35 cowOne.y = 100 cowOne.rotation = 90 cowOne:play() return cowOne end local function cowSpawnOne (event) for i = 0,0 do local c1 = cow() c1.x = 38 c1.y = math.random (600,850) local removeCow = function () display.remove (c1) c1 = nil end cowIntro = transition.to (c1,{time = 23000,y = -50,onComplete = removeCow}) if score == 300 then cowIntro = transition.to (c1,{time = 18000,y = -50,onComplete = removeCow}) if score \>= 300 then cowIntro = transition.to (c1,{time = 15000,y = -50,onComplete = removeCow}) end end end end local function collisionStart (event) if cowOne == nil then return false end if beam == nil then return false end local up = beam.contentBounds.yMin \<= cowOne.contentBounds.yMin and beam.contentBounds.yMax \>= cowOne.contentBounds.yMin local down = beam.contentBounds.yMin \>= cowOne.contentBounds.yMin and beam.contentBounds.yMin \<= cowOne.contentBounds.yMax return (up or down) end local function collisionEnded (event) if cowOne == nil then return false end if beam == nil then return false end local up = cowOne.contentBounds.yMin ~= beam.contentBounds.yMin and cowOne.contentBounds.yMax ~= beam.contentBounds.yMin local down = cowOne.contentBounds.yMin ~= beam.contentBounds.yMin and cowOne.contentBounds.yMin ~= beam.contentBounds.yMax return (up or down) end local function hasCollided (event) if collisionStart (beam,cowOne) then if beam.isVisible == true then cowOne:setSequence ("cowBeaming") cowOne:play() end end end timer.performWithDelay (9200,cowSpawnOne,0) Runtime:addEventListener ("enterFrame",hasCollided)