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
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):
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
Hello
Thanks for all of your responses.
After Claudio first response, I have implemented a solution which is very similar to Ivo's one, excepted I recreate the layer each time the life points change, to allow its width to change.
It works, and will certainly be enough for this game, but I feel it's a bit limited (and I don't even talk of the cost of the solution, which implies to create a new object each time points change).
By example I can't use a gradient as a background color for my lifebar, and crop it as the life points decrease.
I will have a look on Claudio second proposal, but it seems more complicated for a Cocos2d/OpenGL newbie as me.
Using your second implementation (subclassing ColorLayer) I could extend
my existing life bar that was already changing color based on the
percentage of life left, to one that also gets shorter or longer
depending on the life left :)
If somebody wants to see a working example I would probably need to
clean up the code a bit and isolate the needed lines of code from the
rest and put it into one test file, let me know if anyone is interested.
Regards,
Ivo
Excellent, Claudio!
Using your second implementation (subclassing ColorLayer) I could extend
my existing life bar that was already changing color based on the
percentage of life left, to one that also gets shorter or longer
depending on the life left :)
If somebody wants to see a working example I would probably need to
clean up the code a bit and isolate the needed lines of code from the
rest and put it into one test file, let me know if anyone is interested.
Regards,
Ivo
Alright, see attached! It's running in the test directory. It's not
super clean code, and I actually still have a hack there to make the
lifebar disappear when the unit dies, but oh well... you get the idea.
Ivo
Alright, see attached! It's running in the test directory. It's not
super clean code, and I actually still have a hack there to make the
lifebar disappear when the unit dies, but oh well... you get the idea.
Ivo
Thanks for your comments! You are completely right of course, I never
had a separate class for it, until I included your implementation. Of
course I should move the "old" updateBar() function and the other life
bar related code in the Unit class to the LifeBar object. I just quickly
put this together, but I'll put it on my to do list :)
I've been working on this game on and off for the last 3 years, so by
the current speed, you can expect a fully functional game with clean
code by 2016 ;)
> I noticed you included in your code a 'copyright <my name>' for a
> class I posted in this thread. Don't do that please. I understand it
> was meant as a credit, but it hinders code reuse, and annoys people.
> Just saying, not mad at it.
Is it the (c) specifically or the fact that your name is there?
Personally I think it's only just to credit the original authors, and it
never hinders me, but I'm just one person of course :)
Regards,
Ivo
I've been working on this game on and off for the last 3 years, so by
the current speed, you can expect a fully functional game with clean
code by 2016 ;)
Is it the (c) specifically or the fact that your name is there?
> I noticed you included in your code a 'copyright <my name>' for a
> class I posted in this thread. Don't do that please. I understand it
> was meant as a credit, but it hinders code reuse, and annoys people.
> Just saying, not mad at it.
Personally I think it's only just to credit the original authors, and it
never hinders me, but I'm just one person of course :)
Regards,
Ivo
> On Thu, Apr 12, 2012 at 6:21 PM, Ivo F.A.C. Fokkema
> <ivo.f...@gmail.com> wrote:
>
>
>
(snip)
> > I noticed you included in your code a 'copyright <my name>'
> for a
> > class I posted in this thread. Don't do that please. I
> understand it
> > was meant as a credit, but it hinders code reuse, and
> annoys people.
> > Just saying, not mad at it.
>
>
> Is it the (c) specifically or the fact that your name is
> there?
>
>
> The (c) thing specifically
OK, thanks! I will leave it out next time :)
Regards,
Ivo
I reused your code, claudio, because I'm having fun with a grid-based game where some cells can be filled with water at different levels.My problem is that I will display many of those (in a big grid) and want them to be batchable. I'm sorry but I can't find any help on how to make something batchable.
here is a crude attempt : http://pastebin.com/7FYWrDA6The water_block will not be displayed if added to a BatchNode, but will be displayed ok when added to a regular Node...Could someone enlighten me ?
I reused your code, claudio, because I'm having fun with a grid-based game where some cells can be filled with water at different levels.
Is there a way to add z-ordering to this Water_Block class ? I'm sorry for asking yet another question, but I could'nt find a way by myself.If there is no easy way, I'll just add those Water_Blocks to a different Layer.
--
Currently the z order is mandated by:The parent-child relationship between nodesThe z parameter you can pass in cocosnode.add( child, z=...)and the method CocosNode.visit uses that info to drive the render.Look at it to see if your use case can be managed with explicit z.