Player Not Colliding With Enemies

Hello everyone, 

I am having trouble making my enemies collide with the player so that I can transition to my game over screen. I have my player set to “static,” and my enemies set to “dynamic,” and both are set toFront. I also know they are at least touching, but the code is not executing. Any help would be appreciated. 

Could you post up your code rather than attaching a file?

Use code tags to make the forum show your code properly.

To use code tags do this:

[-code-]
Insert your code here in plain text
[-/code-]

Just remove the minus symbols ("-") from within the brackets above for it to work correctly.

Sure, sorry. 
 

Spawning the Player:

function spawnPlayer() player = display.newImage("playership2.png", 50, 50) physics.addBody(player, "static", {density = 1, friction = 0, bounce = 0}) player.x = display.contentWidth \* 0.5 player.y = display.contentHeight - player.height player.name = "player" player.alive = true player:toFront() end

Spawning Enemies:
 

function spawnEnemy() Runtime:addEventListener("collision", onCollision) enemyNumber = enemyNumber + 1 enemies:toFront() enemyGroup[enemyNumber] = display.newImage("enemyship1.png", 50, 50) enemyGroup[enemyNumber].alive = true physics.addBody (enemyGroup[enemyNumber], "dynamic", {density=0.5, friction=0.3, bounce=0}) enemyGroup[enemyNumber].myName = "enemy" end

Collision Function:
 

function onCollision(event) if(event.object1.myName == "player" and event.object2.myName == "enemy") or (event.object1.myName == "enemy" and event.object2.myName == "player") then --code end

Why are you adding a runtime collision listener every time you spawn an enemy?

You only need to add this listener once.

Could you post up your code rather than attaching a file?

Use code tags to make the forum show your code properly.

To use code tags do this:

[-code-]
Insert your code here in plain text
[-/code-]

Just remove the minus symbols ("-") from within the brackets above for it to work correctly.

Sure, sorry. 
 

Spawning the Player:

function spawnPlayer() player = display.newImage("playership2.png", 50, 50) physics.addBody(player, "static", {density = 1, friction = 0, bounce = 0}) player.x = display.contentWidth \* 0.5 player.y = display.contentHeight - player.height player.name = "player" player.alive = true player:toFront() end

Spawning Enemies:
 

function spawnEnemy() Runtime:addEventListener("collision", onCollision) enemyNumber = enemyNumber + 1 enemies:toFront() enemyGroup[enemyNumber] = display.newImage("enemyship1.png", 50, 50) enemyGroup[enemyNumber].alive = true physics.addBody (enemyGroup[enemyNumber], "dynamic", {density=0.5, friction=0.3, bounce=0}) enemyGroup[enemyNumber].myName = "enemy" end

Collision Function:
 

function onCollision(event) if(event.object1.myName == "player" and event.object2.myName == "enemy") or (event.object1.myName == "enemy" and event.object2.myName == "player") then --code end

Why are you adding a runtime collision listener every time you spawn an enemy?

You only need to add this listener once.