Game Development Reference
In-Depth Information
Moving the Bullets
After we shoot the bullets, we also need to move them; otherwise, they'll be stuck in place on the
screen, as shown in Figure
15-3
. When a bullet reaches the end of the screen or collides with an
object, we need to remove it from the screen and make space for a new one.
function moveBullets()
local blt
for i = #bullets, 1, -1 do
blt = bullets[i]
blt.x = blt.x + FAST -- bullets move fast
position(blt.sprite, blt.x, blt.y)
-- check for collision
if blt and blt.x > _W then
destroyObject(blt.sprite)
table.remove(bullets,i)
blt = nil
end
end
Figure 15-3.
The bullets frozen where they were fired
This
moveBullets
function needs to be called as many times as required to keep the bullets moving.
We do this inside of the
update
function, which allows us to call the
moveBullets
function repeatedly.
Figure
15-4
illustrates how this is represented on the screen.
Figure 15-4.
Bullets now moving from the helicopter toward the right of the screen