0

我一直在自学如何使用 Corona 开发手机游戏(到目前为止效果很好!)但我遇到了障碍。我有一个功能可以检查一个人是否撞到硬币。如果他这样做了,硬币将被移除:

function hitCoin()
--Checks for every item in object to see if it collides with the guy
for o = 1, collectables.numChildren, 1 do       
    local left = guy.contentBounds.xMin <= collectables[o].contentBounds.xMin and guy.contentBounds.xMax >= collectables[o].contentBounds.xMin
    local right = guy.contentBounds.xMin >= collectables[o].contentBounds.xMin and guy.contentBounds.xMin <= collectables[o].contentBounds.xMax
    local up = guy.contentBounds.yMin <= collectables[o].contentBounds.yMin and guy.contentBounds.yMax >= collectables[o].contentBounds.yMin
    local down = guy.contentBounds.yMin >= collectables[o].contentBounds.yMin and guy.contentBounds.yMin <= collectables[o].contentBounds.yMax

    --If there is a collision, we remove the object from the object group
    if (left or right) and (up or down) then
        collectables[o]:removeSelf()
        return true;
    end
end

return false;
end

很容易。然而,我也有一个几乎相同的函数来检查子弹是否与敌人相撞。是否可以通过引用传递显示组内的对象?理想情况下,以下函数将能够检查家伙-硬币碰撞和子弹-敌人碰撞之间的关系:

function onCollisionRemoveSecondObject(obj1, obj2)
    local left = obj1.contentBounds.xMin <= obj2.contentBounds.xMin and obj1.contentBounds.xMax >= obj2.contentBounds.xMin
    local right = obj1.contentBounds.xMin >= obj2.contentBounds.xMin and obj1.contentBounds.xMin <= obj2.contentBounds.xMax
    local up = obj1.contentBounds.yMin <= obj2.contentBounds.yMin and obj1.contentBounds.yMax >= obj2.contentBounds.yMin
    local down = obj1.contentBounds.yMin >= obj2.contentBounds.yMin and obj1.contentBounds.yMin <= obj2.contentBounds.yMax

    if (left or right) and (up or down) then
        obj2.isAlive = false
        return true;
    end

return false;
end

for i = 1, collectables.numChildren, 1 do
    onCollisionRemoveSecondObject(guy, collectables[i])
end

for a = 1, bullets.numChildre, 1 do     
    for b = 1, enemies.numChildren, 1 do        
        onCollisionRemoveSecondObject(bullets[a], enemies[b])
    end
end

任何建议或朝着正确方向轻推将不胜感激!

4

2 回答 2

0

除了这个错字,我在您的代码中看不到任何问题:

bullets.numChildre -> bullets.numChildren

当然,您可以像以前一样将组对象的子对象传递给函数。

于 2013-11-07T05:02:42.690 回答
0

I began coding around this function and actually got it to work (somewhat inadvertently). And that nasty little typo didn't help either ;P As a bonus to whomever may read this, I also added a function that cycles through each object in a group and deletes it if the 'isAlive' parameter is false. It's useful in my game, so I hope it helps you guys out too!

function removeObject(object) 
for i = 1, object.numChildren, 1 do
    if object[i].isAlive == false then
        object[i]:removeSelf();
        removeObject(object)
        break;
    end
end
end
于 2013-11-08T03:55:59.213 回答