我一直在自学如何使用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
任何建议或朝着正确方向的推动将不胜感激!
除了这个错别字之外,我在您的代码中没有看到任何问题:
bullets.numChildre -> bullets.numChildren
当然,您可以像以前那样将组对象的子对象传递给函数。
我开始围绕这个函数进行编码,并实际上让它工作(有点无意)。那个讨厌的小错别字也无济于事;P 作为对任何可能阅读本文的人的奖励,我还添加了一个函数,该函数循环遍历组中的每个对象,如果 'isAlive' 参数为 false,则将其删除。它在我的游戏中很有用,所以我希望它也能帮助你们!
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