I've been teaching myself how to use Corona to develop a mobile game (which has been going great so far!) but I've run into a snag. I have a function that checks whether a guy bumps into a coin. If he does, the coin is removed:
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
Easy enough. However, I also have a nearly identical function that checks to see if a bullet collides with an enemy. Is it possible to pass objects within a display group by reference? Ideally, the following function would be able to be check between guy-coin collision and bullet-enemy collision:
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
Any advice or a nudge in the right direction would be greatly appreciated!
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