My event is fired

Hi Brent,

 I manage to make the collision working only with one of the 2 objects (the cowboy),but the cowboy is a global variable. I have a cow that is a local variable,and when the beam colide with it,it gives me an error.Also please check the warning on the console.

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 = 0}} local 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) --line 171 return cowOne end local function cowSpawnOne (event) for i = 0,0 do local cowOne1 = cow() cowOne1.x = 38 cowOne1.y = math.random (600,850) local removeCow = function () display.remove (cowOne) cowOne1 = nil end cowIntro = transition.to (cowOne1,{time = 23000,y = -50,onComplete = removeCow}) if score == 300 then cowIntro = transition.to (cowOne1,{time = 18000,y = -50,onComplete = removeCow}) if score \>= 300 then cowIntro = transition.to (cowOne1,{time = 15000,y = -50,onComplete = removeCow}) end end end end local function cowAnimOne (event) local c1 = cow() -- line 195 c1:setSequence ("cowBeaming") c1:play() end local function cowAnimTwo (event) local c2 = cow() c2:setSequence ("cowOne2") c2:play() end local function beaming (event) if event.phase == "began" then cowAnimOne() -- line 208 end end

Untitled-7.jpg

Do you see the error message at the top about not being able to change it while the world is locked doing calculations?  You cannot call these functions while a collision is still happening.  The usual process is to put that code into a timer.  I don’t see from the code you posted above a good place to put it.  Normally you would have pseudo code like this.

local function handleCollision( event )

      if processingCollision then

              timer.performWithDelay(10, function()  code to deal with spawning your new item; end)

     end

end

or something like this.

Rob

I made it work,but for some weird reason when the beam hits the cow nothing happens.

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 cowOne = cow() cowOne.x = 38 cowOne.y = math.random (600,850) local removeCow = function () display.remove (cowOne) cowOne1 = nil end cowIntro = transition.to (cowOne,{time = 23000,y = -50,onComplete = removeCow}) if score == 300 then cowIntro = transition.to (cowOne,{time = 18000,y = -50,onComplete = removeCow}) if score \>= 300 then cowIntro = transition.to (cowOne,{time = 15000,y = -50,onComplete = removeCow}) end end end end local function beaming (event) if event.phase == "began" then cowOne:setSequence ("cowBeaming") cowOne:play() end end function scene:enterScene (event) local group = self.view local c1 = cow() c1:addEventListener ("collision",beaming)