Hi Pascal,
I had been dealing with that as well, and couldn't get any better than
this (indeed using ColorLayer, what Claudio already suggested):
In unit's __init__():
(snip)
# Now create a life bar (init, will be updated by updateBar).
nBarWidth = int(self.unit.height * 0.9)
self.oBar = cocos.layer.ColorLayer(0, 0, 0, 255, width=nBarWidth, \
height=2) # Default to black.
self.add(self.oBar, z=2)
# You might want to change the position of the bar, for me
# This position works out nicely.
self.oBar.position = (-self.oBar.width/2, self.unit.width/2)
# Add the text (hitpoints left / total life).
self.oBarText = cocos.text.Label('%d / %d' % (self.nHitPoints, \
self.nLife), font_name='Verdana', font_size=6, color=(255, 255, 255, \
150), anchor_x='left', anchor_y='bottom')
self.oBarText.position = (self.oBar.x, self.oBar.y+1)
self.add(self.oBarText, z=2)
self.updateBar()
Then, updateBar() looks like:
def updateBar (self):
if self.nHitPoints < 0:
self.nHitPoints = 0
nDiv = (float(self.nHitPoints)/self.nLife)
if nDiv >= 0.5:
nGreen = 255
nRed = int((1 - nDiv) * 2 * 255)
else:
nGreen = int(nDiv * 2 * 255)
nRed = 255
# Now draw it.
self.oBar.color = (nRed, nGreen, 0)
self.oBarText.element.text = '%d / %d' % (self.nHitPoints, \
self.nLife)
The bar nicely moves from green to orange to red, if the life of the
unit decreases. However, what I've been struggling with and couldn't
implement, is trying to change the width of the health bar. I couldn't
get that to work. If you find out how, I would be very grateful if you'd
tell me how :)
Good luck on your game!
Ivo
--
You received this message because you are subscribed to the Google Groups
"cocos2d discuss" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/cocos-discuss?hl=en.