0
votes

I am currently coding a game in Corona SDK, and have run into a problem regarding the collisions for my player and enemies. For some reason, I cannot get the code associated with their collision to execute. The code for the bullets and enemies colliding does work. I'm not sure why. Has anyone run into a similar problem? I have included the code in question.

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
1
This isn't a good question. You're basically asking others to debug your code - You should extract the important / corresponding parts and include them in your question. - Appleshell
I apologize, I've edited it and placed the relevant parts in the question. - user3456833
And what about that isn't working? What values are you getting for event.object1 and event.object2 in that collision callback? Or is the problem that your callback isn't being run at all? - Etan Reisner
That is the problem, yes. The enemies just push the player around. So I know that they are at least touching, it's just none of my code is executing at all. - user3456833
Create a minimal self-contained example that shows the problem. As it is, it is not possible to tell what problem is because we don't know what code calls the spawn. And do you mean "code for the bullets and enemies colliding does NOT work"? - Oliver

1 Answers

1
votes
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.**myName** = "player"
player.alive = true
player:toFront()
end