I have a this health bar, everything works as it should, the only problem is when I change the HP the width changes as well. How can I stop that?
Here’s the code I use.
[lua]
–creates the health bar background
local barBack = display.newRect(0, 0, user.maxHealth + 10 , 25)
magnet:left(barBack, 25)
magnet:top(barBack, 10)
hudGroup:insert(barBack)
barBack:setFillColor( 100/255, 100/255, 100/255 )
–creates the health bar
local healthBar = display.newRect(0, 0, user.maxHealth, 20)
magnet:left(healthBar, healthBar.width/2 + 30)
healthBar.y = barBack.y
healthBar.anchorX = 1
hudGroup:insert(healthBar)
healthBar:setFillColor( 255/255, 0/255, 0/255 )
–when the player gets damaged
local damageBar = display.newRect(0, 0, 0, 20)
damageBar.x = healthBar.x
damageBar.y = healthBar.y
damageBar.anchorX = 1
hudGroup:insert(damageBar)
damageBar:setFillColor( 60/255, 60/255, 60/255 )
–displays the player health
local healthText = display.newText(hudGroup, playerTable.health…"/"…user.maxHealth, 0, 0, _font, 12)
healthText.x = barBack.x
healthText.y = barBack.y
healthText.alpha = 0.8
heart:toFront( )
–updates the health bar
function updateDamageBar()
damageBar.width = user.maxHealth - playerTable.health
healthText.text = playerTable.health…"/"…user.maxHealth
end
[/lua]