我一直在自学如何使用 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
任何建议或朝着正确方向轻推将不胜感激!