My event is fired

For some unknown isues my collision events aren’t fired.The collision takes place,but the individual functions do not work,( the cowboyCollision and the cowOneCollision).Thank you!

local function cowboyCollision (event) if event.phase == "began" then print ("you hit a cowboy") end end local function cowCollision (event) if event.phase == "began" then print ("you hit a cow") end end function scene:enterScene (event) local group = self.view local cowOne = cow() local cowboy = cowboy() cowboy.collision = cowboyCollision Runtime:addEventListener ("collision",cowboy) cowOne.collision = collision Runtime:addEventListener ("collision",cowOne) . . .

Hi @bogdanmocanu2,

It looks like you’re mixing up the syntax/usage for “global” (Runtime) vs. “local” (table listener) collision handling. Please inspect the guide and examples under “Collision Handling”:

http://docs.coronalabs.com/guide/physics/collisionDetection/index.html#handling

Best regards,

Brent

Hi Brent, 

  I tried this,but it doesn’t work. The collision takes place,but the collision event isn’t fired.

local function cow (event) local sheetData = {width = 100,height = 100,numFrames = 8,sheetContentWidth = 200,sheetContentHeight = 400} local mySheet = graphics.newImageSheet ("CowWalk.png",sheetData) local sequenceData = {name = "cowOne",start = 1,count = 8,time = 950,loopcount = 0,looDirection = "forward"} local cowOne = display.newSprite (mySheet,sequenceData) cowOne.x = 35 cowOne.y = 700 cowOne.rotation = 90 cowOne:play() physics.addBody (cowOne,"static",{friction = 0.2,radius = 22}) return cowOne end . . . local function cowOneCollision (self,event) if event.phase == "began" then print ("You hit a cow") elseif event.phase == "ended" then print ("Collision ended") end end function scene:enterScene (event) local group = self.view local cowOne = cow() cowOne.collision = cowOneCollision cowOne:addEventListener ("collision",cowOne) . . .

I think I found a method.If I move the cowOne to the scene:createScene function and convert it from local to global variable,the collision events is fired.But there’s a catch.

I need the cow function to spawn random cows around the scene and if I delete it/move it the cowSpawnOne function will not work anymore…What should I do in this case?

Here’s the function that I am talking about.Thank you.

local function cowSpawnOne (event) for i = 0,0 do cowOne = cow() cowOne.x = 38 cowOne.y = math.random (600,850) local removeCow = function () display.remove (cowOne) cowOne = 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

Hi @bogdanmocanu2,

You should definitely find a way to avoid global variables/functions. If you need to access a function in other “phases” of Composer, that function should be placed outside of the individual blocks like “createScene”. Remember that you can up-value functions if necessary.

Brent

Hi Brent,

All the local functions are placesd outside the indiidual blocks.The eventListeners are in the “enterScene” function.

'till now I couldn’t find a way to trigger my collision event(s),What is the solution? I do not think I can work with only local variables,that’s almost imposible.

I mean,I have an object that is used by 3 other local functions.

Thanks!

I’m having some trouble understanding where your calls are.  Can you post a more complete version of your scene. Also when you post your code, it will help to have things properly indented.

Thanks

Rob

Hi Rob,

 Here’s the code.My local functions are not declared in any storyboard scene.Also my setSequence doesn’t work for the cowboy. Thank you.

. . . local function cowboy (event) local sheetData = {width = 70,height = 107,numFrames = 8,sheetContentWidth = 140,sheetContentHeight = 428} local mySheet = graphics.newImageSheet ("CowboyWalking.png",sheetData) local sequenceData = {name = "cowboy2",start = 1,count = 8,time = 1100,loopcount = 0}, {name = "cowboyShooting",start = 9,count = 14,time = 1200,loopcount = 0}  local cowboy = display.newSprite (mySheet,sequenceData) cowboy.x = 50 cowboy.y = 700 cowboy.rotation = 90 cowboy:setSequence ("cowboyShooting") cowboy:play() physics.addBody (cowboy,"static",{friction = 0.2,radius = 18 }) return cowboy end local function cowboySpawn (event) for i = 0,0 do local cowboy = cowboy() cowboy.x = 50 cowboy.y = math.random (600,950) local removeCowboy = function() display.remove (cowboy) cowboy = nil end cowboyIntro = transition.to (cowboy,{time = 19000,y = -50,onComplete = removeCowboy}) end end local function cow (event) local sheetData = {width = 100,height = 100,numFrames = 8,sheetContentWidth = 200,sheetContentHeight = 400} local mySheet = graphics.newImageSheet ("CowWalk.png",sheetData) local sequenceData = {name = "cowOne",start = 1,count = 8,time = 950,loopcount = 0,looDirection = "forward"} local cowOne = display.newSprite (mySheet,sequenceData) cowOne.x = 35 cowOne.y = 700 cowOne.rotation = 90 cowOne:play() physics.addBody (cowOne,"static",{friction = 0.2,radius = 22}) 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) cowOne = 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 cowOneCollision (self,event) if event.phase == "began" then print ("You hit a cow") elseif event.phase == "ended" then print ("Collision ended") end end -- Normal function ends here. -- This is the enterScene function.We can add event listeners and other things. function scene:enterScene (event) local group = self.view local cowOne = cow() local cowboy = cowboy() cowOne.collision = cowOneCollision cowOne:addEventListener ("collision",cowOne) . . . --some other events listeners.

 A little help with this mate,please.

Your cowboy sequence problem appears to be a syntax problem.  When you have multiple sequences, each sequence is a table and all tables have to be within another table.  See the syntax here:  http://docs.coronalabs.com/api/library/display/newSprite.html#multiple-sequences.

For your collision issues, I would suggest putting some print statements to before you setup your collision event handlers to make sure you’re getting there.  Then in the event functions themselves, I would print the “event” table and make sure you’re getting the values you expect there.  See:  http://coronalabs.com/blog/2014/09/02/tutorial-printing-table-contents/

Rob

local function cowboy (event) local sheetData = {width = 70,height = 107,numFrames = 14,sheetContentWidth = 140,sheetContentHeight = 749} local mySheet = graphics.newImageSheet ("cowboy.png",sheetData) local sequenceData = {name = "cowboyWalking", start = 1,count = 8,time = 1200,loopcount = 0}, {name = "cowboyShooting",frames = {9,10,11,12,13,14},time = 1000,loopcount = 1} local cowboy = display.newSprite (mySheet,sequenceData) cowboy.x = 50 cowboy.y = 70 cowboy.rotation = 90 cowboy:setSequence ("cowboyShooting") cowboy:play() physics.addBody (cowboy,"static",{friction = 0.2,radius = 18 }) return cowboy end 

  I have updated the code on the cowboy function,but still doesn’t work. 

I fixed the setSequence on the animation isue. The only isue left now is the collision.

The method you showed me (with the tables) do not work.

I found that if I convert the local var to global var. the collision event is fired.But the only problem then is the spawing.The cowboy doesn’t spawn.

local spawnTable = {} local function onSpawn (event) for i = 1,2 do spawnTable [i] = cowboy end end

Hi @bogdanmocanu2,

At this time, I suggest that you clean up your code a little. I suspect that the issue is still in your syntax.

  1. Most importantly, don’t use global variables and objects. This is going to cause problems, and there are several ways to avoid using globals in any project.

  2. Do not name your variables and functions the same. For example, you have a function “cowboy” and also a local variable “cowboy” within the enterScene block. This is probably causing conflicts in Lua. You need to be more specific when naming things, not only to produce functional code, but also for clarity when you (or we) read your code. So, change the function “cowboy()” to something more specific like “spawnCowboy()”, and fix every place that you reference it. Then, within the functions, instead of doing “local cowboy = (something)”, maybe state it like “local newCowboy = (something)” instead.

Best regards,

Brent

This is incorrect:

local sequenceData = {name = "cowboyWalking", start = 1,count = 8,time = 1200,loopcount = 0}, {name = "cowboyShooting",frames = {9,10,11,12,13,14},time = 1000,loopcount = 1}

It should be this:

local sequenceData = { {name = "cowboyWalking", start = 1,count = 8,time = 1200,loopcount = 0}, {name = "cowboyShooting",frames = {9,10,11,12,13,14},time = 1000,loopcount = 1} }

Thanks guys!

Hi Brent,

 I fixed almost everything,but I have a problem with my preCollision and postCollision events.It gives me an error. Also when the cowboy hits the beam,the beam gets out of its original possition and I do not want that to happen.How do I prevent this?

Thanks.

local function cowboy:preCollision (event) if beam.isVisible == false then event.contact.isEnabled = false end end local function cowboy:postCollision (event) beam:removeEventListener("preCollision") beam:removeEventListener ("postCollision") end Runtime:addEventListener ("postCollision") Runtime:addEventListener ("preCollision")

Untitled-6.jpg

Hi @bogdanmocanu2,

Is there a specific reason why you’re using preCollision and postCollision? This type of physics collision handling is usually only needed for advanced scenarios, and the use of “physicsContact” is powerful but isn’t needed for most games.

It seems like the “beam” should be a sensor object. Have you made it so? This will cause the beam to detect collisions, but it will not react (physically) with other objects… meaning, it won’t be pushed out of position when it collides with the cowboy.

Best regards,

Brent

Hi Brent,

  If I make the beam a sensor,then the collision event won’t trigger anymore.Anyother options? Or should I use the nonphysics collision (overlapping rectangles. http://coronalabs.com/blog/2013/07/23/tutorial-non-physics-collision-detection/). All I am afraid of is the game lag.I do not want my game to lag.

Cheers,

Bogdan

beam = display.newImageRect("Beam.png" ,90,200) beam.rotation = 90 beam.isVisible = false physics.addBody (beam,"dynamic") beam.isSensor = true beam:setLinearVelocity (0,0) . . . -- When the cowboy will colide with the beam.The shooting animation will ocur.WHen the collision ended the walking animation will be triggered. local function onCollision (event) if event.phase == "began" or beam.isVisible == true then cowboy:setSequence ("shooting") cowboy:play() end end local function ofCollision (event) if event.phase == "ended" then cowboy:setSequence ("cowboyWalking") cowboy:play() end end

Hi @bogdanmocanu2,

Simply changing something to a sensor will not affect collision detection, so there must be something else incorrect in your setup. If two non-sensor objects detected collision before, they will detect collision as sensors.

Brent

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