Something like this:
[lua]local kHealthBarWidth = 500
local kHealthBarHeight = 80
local lastHealth = -1
local currHealth = 100
local healthBar = display.newRect( 0,0,kHealthBarWidth ,kHealthBarHeight )
healthBar.x = 8
healthBar.y = 8
healthBar:setFillColor( 0,0,0 )
– Set the fill color to a percent of full red or blue
– amount can be 0-100
– 0 (no health) would give us 100% red and 0% blue
– 100 (full health) would give us 0% red and 100% blue
function updateHealth( amount )
– this check is to make sure we don’t update the display
– unless it has actually changed
if ( lastHealth ~= amount ) then
lastHealth = amount
healthBar:setFillColor( math.round( 255*(100-amount)/100),0,255*amount/100) )
end
end[/lua]
Then whenever you need to update the Health meter you can just set it to whatever percent you want. For instance, if you want the health to start full and go down to 0 over some number of seconds you can do this:
[lua]local kTimeToDie = 10000 – 10 seconds which is 10000 milliseconds
local kHealthDecTime = kTimeToDie / 100
local function healthListener( event )
currHealth = currHealth - 1
updateHealth( currHealth )
end
timer.performWithDelay( kHealthDecTime, healthListener )[/lua]
NOTES: updateHealth() takes a parameter so that you can call it at the end of your scene setup to show the initial full health. If you don’t want to do this then you can remove that and just use the “currHealth” value directly
I’ve just written this off the cuff, so there may be some minor errors, but I wanted to show you the general concepts. Let me know if anything isn’t clear and I can throw together a small sample app showing this actually working. [import]uid: 16734 topic_id: 19724 reply_id: 76337[/import]