Game Development Reference
In-Depth Information
Collisions
The last thing left in the grand scheme of things is to detect when the helicopter collides with any
of the scenery items, which will crash it, and when the helicopter collides with a survivor, which will
save the survivor.
function checkCollisions()
-- create the player rect local hx, hy = getPosition(heli) pRect = {
x = hx,
y = hy,
wd = heli.width,
ht = heli.height,
}
-- find if the helicopter collided with a scenery object
for i = #scenery, 1, -1 do
local nme = scenery[i]
if nme.objType == objects.plane or nme.objType == objects.balloon or
nme.objType == objects.lamppost or nme.objType == objects.house or
nme.objType == objetcs.tallHouse or nme.objType == objects.angryCloud then
hx, hy = getPosition(nme.sprite)
nRect = {
x = hx,
y = hy,
wd = nme.sprite.width,
ht = nme.sprite.height,
}
if collides(nRect, pRect) == true then
reduceLife()
break
end
end
end
hx, hy = getPosition(survivor)
sRect = {
x = hx,
y = hy,
wd = hx + survivor.width,
ht = hy + survivor.height,
}
-- check if we have collected the survivor
if collides(sRect, pRect) == true then
collected = collected + 1
updateText(txtSaved, collected)
setVisible(survivor, false)
score = score + 100
updateText(txtScore, score)
playSound(sounds.collectSurvivor)
end
 
Search WWH ::




Custom Search