玩家碰撞不起作用 - Corona SDK,Lua

时间:2014-05-06 00:25:34

标签: lua corona collision

我目前正在使用Corona SDK编写游戏代码,并且遇到了有关我的玩家和敌人的碰撞的问题。出于某种原因,我无法获得与其碰撞相关的代码来执行。子弹和敌人相撞的代码确实有效。我不确定为什么。有没有人遇到过类似的问题?我已经包含了相关代码。

产生播放器:

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

产卵敌人:

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

碰撞功能:

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 个答案:

答案 0 :(得分:1)

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