Game Development Reference
In-Depth Information
-- check if we have collected the fuel
if tanker~= nil then
hx, hy = getPosition(tanker)
tRect = {
x = hx,
y = hy,
wd = hx + tanker.width,
ht = hy + tanker.height,
}
if collides(tRect, pRect) == true then
fuel = MAX_FUEL
updateText(txtFuel, fuel)
setVisible(tanker, false)
-- reposition the tanker off the screen
position(tanker, _W + random(3,5) * tanker.width, baseLine - tanker.height)
playSound(sounds.collectFuel)
end
end
collides is a simple function that checks if the rectangles bounding the sprites overlap.
overlappingRectangle function called rectOverlaps , which is discussed in
function collides(rect1, rect2)
local x,y,w,h = rect1.x,rect1.y, rect1.wd, rect1.ht
local x2,y2,w2,h2 = rect2.x,rect2.y, rect2.wd, rect2.ht
return not ((y+h < y2) or (y > y2+h2) or (x > x2+w2) or (x+w < x2))
end
To make things interesting, we can also show/hide the crash graphic in place of the helicopter every
time it crashes into an object or the ground, as follows:
hx, hy = getPosition(heli)
setVisible(heli, false)
position(crash, hx, hy)
setVisible(crash, true)
Later, in the replay screen displayed by the showReplayScreen function, we can remove the crash
graphic and set the helicopter back to visible, as follows:
setVisible(crash, false)
setVisible(heli, true)
position(heli, _W/2, _H/2)
Shooting Planes and Balloons
Earlier, we spawned bullets and made them move toward the right side of the screen. Now we have
to handle cases in which the bullets hit something, in which case we remove the objects that are hit.
 
Search WWH ::




Custom Search