Game Development Reference
In-Depth Information
if theHealth > 0 then
healthBar.width = (barSize * theHealth/10)
end
end
Runtime:addEventListener ( "tap", updateHealth )
On running this code, as shown in Figure
8-15
, a green bar is displayed on the screen. It gets smaller
every time we tap the screen and changes color from green to yellow to red. In a real game, this
health bar might be smaller and linked to each character, and multiple bars might be used—one
linked to each character.
Figure 8-15.
A health bar displayed on the screen (currently at full capacity)
To make the bar smaller, you can simply tweak the
barSize
and
barHeight
to the values of your
choice. You can also pass the
healthBar
to the
updateHealth
function to manage multiple health bars.
Finally, you may want to create a
healthBar
object that is self-contained and works with its own set of
functions and properties, like a widget. I'll leave these last options for you to try on your own.