Collision not working?

Hello,

I used this exact code/method for another one of my projects and it worked perfectly fine. But in this new project, I’m trying to implement a lock and key and it won’t work. Basically, the player collides with the key, key disappears and if player collides with the lock while playerHasKey = true, the lock disappears.

However, when the player collides with the key it does nothing. It won’t even print in the console.

Here’s the code:

local myPlayer = display.newImageRect("/images/HUD/player1.png", 50, 50)

myPlayer.x = display.contentCenterX - 150

myPlayer.y = display.contentCenterY - 100

playerHasKey = false

physics.addBody( myPlayer, “dynamic”, {density = 1.0, radius = 25, friction = 0.7, bounce = 0.3})

myPlayer.name = “myPlayer”

local keyBlue = display.newImageRect("/images/Items/keyBlue.png", 75, 65)

keyBlue.x = display.contentCenterX - 100

keyBlue.y = display.contentCenterY

keyBlue.objType = “key”

keyBlue.myName = “keyBlue”

physics.addBody(keyBlue, “dynamic”)

local lockBlue = display.newImageRect("/images/Tiles/lock_blue.png", 65, 75)

lockBlue.anchorX = 0.0

lockBlue.x = display.contentCenterX + 100

lockBlue.y = display.contentCenterY

lockBlue.objType = “lock”

lockBlue.myName = “lockBlue”

physics.addBody(lockBlue, “dynamic”, {density = 1.0, friction = 0.8, bounce = 0.3})

function grabKey(event)

if(event.object1.myName == “myPlayer” and event.object2.myName == “keyBlue”) then

print(“Player has grabbed the key!”)

playerHasKey = true

event.object2:removeSelf()

end

end

Runtime:addEventListener( “collision”, grabKey)

function playerWins(event)

if(event.object1.myName == “myPlayer” and event.object2.myName ==“lockBlue”) then

if (playerHasKey == true ) then

event.object2:removeSelf()

end

end

end

Runtime:addEventListener(“collision”, playerWins)

myPlayer has another event listener for touch which enables drag and drop. I thought that might be conflicting, but I commented it out and the problem is still there. Not sure what else it could be. I’m using the same exact code from another project.

Hi @nickthelibra,

I suggest a few things here:

  1. If you’re using Runtime (“global”) collision detection, you should not have multiple event listeners/functions checking for this. You should only use one master listener/function to detect all collisions in the game.

  2. You need to check for the “phase” of collisions, since each one will trigger both a “began” and “ended” phase, which could disrupt your conditional logic. Most often the “began” phase is the best place to check, since it indicates the moment when the collision occurred, but in some cases the “ended” phase must be handled too.

Take care,

Brent

Thanks for the response.

Not sure how to create one master event listener, but my guess is to add them all to one event listener by using commas?

Such as:

Runtime:addEventListener(“collision”, function1, function2, function3) etc?

Again, I’m just taking a stab in the dark. Still very new to this.

Can you give an example of checking for phases in my collision event?

I’m really trying to understand this language. This is something I enjoy doing, always wanted to do and I eventually want to turn it into a career. I appreciate the help you provide to the community, Brent (and everyone else who contributes to the community).

This issue has been resolved (in a weird way). Can someone explain this?

Basically, I changed the order of objects in the collision function. Instead of myPlayer being object1 and keyBlue being object 2, I switched them around. Why did that fix the problem? I didn’t think it mattered.

I still would like to know how to make this “master” collision event.

Hi @nickthelibra,

From the description of your project, it sounds like you may want to use “local” collisions instead of “global” (Runtime). Local are best used when you have a 1-to-many type of relationship, like 1 player which will interact (collide) with 1+ other objects/enemies etc. Global are best used when you have a many-to-many relationship, like dozens of characters which can interact (collide) with dozens of other objects/enemies etc. Your scenario sounds more like the first (local) pattern to me.

The guide outlines both types of collision handling and how to properly set them up.

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

Best regards,

Brent

Hi @nickthelibra,

I suggest a few things here:

  1. If you’re using Runtime (“global”) collision detection, you should not have multiple event listeners/functions checking for this. You should only use one master listener/function to detect all collisions in the game.

  2. You need to check for the “phase” of collisions, since each one will trigger both a “began” and “ended” phase, which could disrupt your conditional logic. Most often the “began” phase is the best place to check, since it indicates the moment when the collision occurred, but in some cases the “ended” phase must be handled too.

Take care,

Brent

Thanks for the response.

Not sure how to create one master event listener, but my guess is to add them all to one event listener by using commas?

Such as:

Runtime:addEventListener(“collision”, function1, function2, function3) etc?

Again, I’m just taking a stab in the dark. Still very new to this.

Can you give an example of checking for phases in my collision event?

I’m really trying to understand this language. This is something I enjoy doing, always wanted to do and I eventually want to turn it into a career. I appreciate the help you provide to the community, Brent (and everyone else who contributes to the community).

This issue has been resolved (in a weird way). Can someone explain this?

Basically, I changed the order of objects in the collision function. Instead of myPlayer being object1 and keyBlue being object 2, I switched them around. Why did that fix the problem? I didn’t think it mattered.

I still would like to know how to make this “master” collision event.

Hi @nickthelibra,

From the description of your project, it sounds like you may want to use “local” collisions instead of “global” (Runtime). Local are best used when you have a 1-to-many type of relationship, like 1 player which will interact (collide) with 1+ other objects/enemies etc. Global are best used when you have a many-to-many relationship, like dozens of characters which can interact (collide) with dozens of other objects/enemies etc. Your scenario sounds more like the first (local) pattern to me.

The guide outlines both types of collision handling and how to properly set them up.

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

Best regards,

Brent