collision target

I am tryng to build a collision between 3 objects.A laser beam,a cow and a cowboy. All good,but when the collision it is fired,it gives me an error that says “attepmt to index field  ‘target’ a nil value”.The target is the beam,but how do I declare it? Thank you!

local function onCollision (event) local cowOne = cow() cowOne.type = "cowOne" cowOne.collision = onCollision beam.type = "beam" beam.collision = onCollision if event.phase == "began" then if event.target.type == "beam" and event.other.type == "cowOne" then print ("You hit a cow") else print ("You hit a cowboy") end end end

Hi @bogdanmocanu2,

What is “beam”? Is it a physics object? Where do you declare it? Does it have a collision detection listener on it?

Brent

Hi @Brent,

Here is the updated code.The cowOne,cowboy and the beam are physics objects.The cowOne and the cowboy are static objects and the beam is a dynamic object.The collision takes place,but nothing is triggered.

local function cowCollision (event) if event.phase == "began" then  print ("You hit a cow") end end local function cowboyCollision (event) if event.phase == "began" then  print ("you hit a cowboy") end end -- the events are fired in the enterScene function (I use storyboard.The local functions are not declared inside a storyboard function,or anyother function.) local cowOne = cow () local cowboy = cowboy() cowOne:addEventListener ("collision",cowCollision) cowboy:addEventListener ("collision",cowboyCollision)

I can send you the whole code via a pm message.Also,how do I add preCollision and postCollision?

Hi @bogdanmocanu2,

I replied to your other post on this. Here, as in the other post, it looks like you’re confusing the syntax for the collision event listeners.

Best regards,

Brent

Hi @bogdanmocanu2,

What is “beam”? Is it a physics object? Where do you declare it? Does it have a collision detection listener on it?

Brent

Hi @Brent,

Here is the updated code.The cowOne,cowboy and the beam are physics objects.The cowOne and the cowboy are static objects and the beam is a dynamic object.The collision takes place,but nothing is triggered.

local function cowCollision (event) if event.phase == "began" then  print ("You hit a cow") end end local function cowboyCollision (event) if event.phase == "began" then  print ("you hit a cowboy") end end -- the events are fired in the enterScene function (I use storyboard.The local functions are not declared inside a storyboard function,or anyother function.) local cowOne = cow () local cowboy = cowboy() cowOne:addEventListener ("collision",cowCollision) cowboy:addEventListener ("collision",cowboyCollision)

I can send you the whole code via a pm message.Also,how do I add preCollision and postCollision?

Hi @bogdanmocanu2,

I replied to your other post on this. Here, as in the other post, it looks like you’re confusing the syntax for the collision event listeners.

Best regards,

Brent