What am I doing wrong?

   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)

Hi @bogdanmocanu2,

Are you still using global objects? Basically, this scenario will not work properly until you stop using globals. I mentioned this a few times in your previous posts, and you must eliminate use of global physics objects/listeners. I suggest that you start fresh, eliminate all global objects, and then carefully study how to use physics for a “multiple spawned items” game.

Best regards,

Brent

Hey Brent, Each object will be used individually by at least 5 diferent functions and using only local var will be a little tricky. Can you please provide me with a link on how to work with multiple spawned objects? Thank you.

Read up on Object Orientated programming. This allows you to have multiple ‘cows’ all of which share the same functionality.

Can you please give me al ink? Thanks.

You might want to start here:  and learn how to use a data module to make fake globals.  But I think in this case, even with that you may be creating objects that are stomping on each other.  Google Lua Object oriented  and see what comes up.

I did it! Thank you guys.

I followed this tutorial (http://lazywombat.co.uk/object-oriented-programming-in-corona-sdk-part-1-basics/)

But I also need a collision function,and I need the event listener to be triggered by the crate.how do I do that?

I tried to declare a new variable,but it doesn’t work.

-- This is the Crate.lua file Crate = {} function Crate:new() local self = display.newGroup() self:insert(display.newImage("Icon.png")) return self end

 --------------------------------------

-- This is the main.lua file (only for this experiment) require ("Crate") local physics = require ("physics") physics.start() physics.setGravity (0,0) local function onSpawn (event) for i = 0,0 do local crate = Crate:new() physics.addBody (crate,"static") crate.x = math.random (50,300) crate.y = 450 local removeCrate = function () display.remove (crate) crate = nil end cIntro = transition.to (crate,{time = 20000,x = 0,onComplete = removeCrate}) end end timer.performWithDelay (9000,onSpawn,0) local beam = display.newImageRect("Beam.png" ,90,200) physics.addBody (beam,"dynamic") beam.isSensor = true beam:setLinearVelocity (0,0) beam.isVisible = false local function onCollision (event) if event.phase == "began" then print ("It works!") end end Runtime:addEventListener ("collision",onCollision)

Some sugestions with my last post? Thanks.

Hi @bogdanmocanu2,

Are you still using global objects? Basically, this scenario will not work properly until you stop using globals. I mentioned this a few times in your previous posts, and you must eliminate use of global physics objects/listeners. I suggest that you start fresh, eliminate all global objects, and then carefully study how to use physics for a “multiple spawned items” game.

Best regards,

Brent

Hey Brent, Each object will be used individually by at least 5 diferent functions and using only local var will be a little tricky. Can you please provide me with a link on how to work with multiple spawned objects? Thank you.

Read up on Object Orientated programming. This allows you to have multiple ‘cows’ all of which share the same functionality.

Can you please give me al ink? Thanks.

You might want to start here:  and learn how to use a data module to make fake globals.  But I think in this case, even with that you may be creating objects that are stomping on each other.  Google Lua Object oriented  and see what comes up.

I did it! Thank you guys.

I followed this tutorial (http://lazywombat.co.uk/object-oriented-programming-in-corona-sdk-part-1-basics/)

But I also need a collision function,and I need the event listener to be triggered by the crate.how do I do that?

I tried to declare a new variable,but it doesn’t work.

-- This is the Crate.lua file Crate = {} function Crate:new() local self = display.newGroup() self:insert(display.newImage("Icon.png")) return self end

 --------------------------------------

-- This is the main.lua file (only for this experiment) require ("Crate") local physics = require ("physics") physics.start() physics.setGravity (0,0) local function onSpawn (event) for i = 0,0 do local crate = Crate:new() physics.addBody (crate,"static") crate.x = math.random (50,300) crate.y = 450 local removeCrate = function () display.remove (crate) crate = nil end cIntro = transition.to (crate,{time = 20000,x = 0,onComplete = removeCrate}) end end timer.performWithDelay (9000,onSpawn,0) local beam = display.newImageRect("Beam.png" ,90,200) physics.addBody (beam,"dynamic") beam.isSensor = true beam:setLinearVelocity (0,0) beam.isVisible = false local function onCollision (event) if event.phase == "began" then print ("It works!") end end Runtime:addEventListener ("collision",onCollision)

Some sugestions with my last post? Thanks.