Hi, I´ve got problem with HP bar in my game. It works fine when player loses HP, but it does nothing when HP are gained. I have tested, if HP are realy gained by some display functions and special events, and everything except the HP bar works fine. Here is the code for drawing HP bar from the repeteadly_execute part of script (it displays how many HP has player lost, not how many HP does he has, btn_player_health height and width is 70 pixels): NumA=IntToFloat(player_maxhealth);
NumB=IntToFloat(player_health);
NumC=NumA /70.00;
if (NumC==0.00) NumC=0.01;
NumF=NumB/NumC;
NumD=FloatToInt(NumF, eRoundUp);
HP_bar = DynamicSprite.CreateFromExistingSprite(btn_player_health.NormalGraphic);
DrawingSurface*ds = HP_bar.GetDrawingSurface();
ds.DrawImage(0, 0, 74);
ds.DrawingColor = Game.GetColorFromRGB(255,0,0); // red
ds.DrawRectangle(0, 70, 70, NumD); // draw
ds.Release(); // end drawing
btn_player_health.NormalGraphic = HP_bar.Graphic;
btn_player_health.Text = String.Format("%d/%d", player_health, player_maxhealth);
I am sure I have done just some stupid fault, but I cannot find it! :-\
Using Button clipping is usually the easiest way to do health....
[Edit]: I'm not great at Drawing code, but the line with ds.DrawRectangle is striking me as odd... lemme try to check the manual entry and see if I'm crazy or not.
[Edit2]: Why is HP_bar created from the existing sprite of the button? Why not create it from sprite 74 directly? (Assuming I understand correctly)
~Trent
I've tried decrypting the first six lines of code ;)
NumD = (player_health * 70) / player_maxhealth;
is all you need to do.
Now, since you're not drawing the part of the health bar that represents health but the "hole", loosing HP makes the hole bigger and thus visibly changes the image while gaining HP doesn't (because you're drawing over the old bar image).
HP_bar = DynamicSprite.CreateFromExistingSprite(74);
DrawingSurface*ds = HP_bar.GetDrawingSurface();
ds.DrawingColor = Game.GetColorFromRGB(255,0,0); // red
if (NumD < 70) ds.DrawRectangle(0, NumD, 70, 70); // draw
ds.Release(); // end drawing
btn_player_health.NormalGraphic = HP_bar.Graphic;
This should work.
Oh, thanks, this works great! :) It ends like that when I´m copying code from my older games with just a minor changes... ;)
It's from KhrisMUC, it's almost unnessecary to tell HIM that it's working great :P
Yes, you´re right, just my habit... ;D