在Lua/Corona中传递显示组对象byref?
我一直在教自己如何使用Corona开发一款手机游戏(到目前为止进展很好!),但我遇到了一个障碍。我有一个函数可以检查一个人是否撞到硬币上。如果他这样做,硬币将被移除:在Lua/Corona中传递显示组对象byref?,lua,pass-by-reference,coronasdk,displayobject,byref,Lua,Pass By Reference,Coronasdk,Displayobject,Byref,我一直在教自己如何使用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.contentB
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
函数hitCoin()
--检查对象中的每个项目,以查看它是否与guy发生碰撞
对于o=1,collectables.numChildren,1 do
local left=guy.contentBounds.xMin=collectables[o].contentBounds.xMin
local right=guy.contentBounds.xMin>=collectables[o]。contentBounds.xMin和guy.contentBounds.xMin=collectables[o]。contentBounds.yMin和guy.contentBounds.yMin=obj2.contentBounds.xMin=obj2.contentBounds.yMin和obj1.contentBounds.yMin我看不出代码中有任何问题,除了这个输入错误:
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
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