Game Development Reference
In-Depth Information
yPos = TOPLINE + random(1,5) * spr.height
speed = random(NORMAL, FAST)
end
xPos = _W + random(3,8) * spr.width
position(spr, xPos, yPos)
table.insert(scenery,{
sprite = spr,
speed = speed,
x = xPos,
y = yPos,
dir = yDir,
wd = spr.width,
ht = spr.height,
objType = rnd
})
end
Moving the Scenery Objects
In the previous section, we spawned the 11 objects, but set their x positions to a position off the
screen. In this way, the items will start from a location off the screen and scroll in gradually, rather
than suddenly appearing on the screen. However, we have not written the code to scroll the scenery
as yet. In our update function, we need to add moveScenery() after the moveBullets function. The
idea is similar to that of the moveBullets function. In this function, however, in addition to moving the
items to provide the illusion of our helicopter moving through the sky as the objects pass by, we also
update the distance travelled and the fuel used. When we reach a critical reserve of 100 fuel units,
we display the tanker, which can be collected to refuel.
function moveScenery()
for i = #scenery, 1, -1 do
local nme = scenery[i]
nme.x = nme.x - nme.speed
nme.y = nme.y + nme.dir
if nme.y < TOPLINE or nme.y > BOTTOMLINE then
nme.dir = -nme.dir
end
local rnd = random(1,10)
if rnd > 3 and rnd < 4 then
nme.dir = -nme.dir
end
position(nme.sprite, nme.x, nme.y)
if nme.x < -nme.wd then
destroyObject(nme.sprite)
table.remove(scenery, i)
end
end
 
Search WWH ::




Custom Search