Drawing HP does not work properly (using DrawingSurface) [SOLVED]

Started by .M.M., Tue 26/05/2009 19:23:58

Previous topic - Next topic

.M.M.

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):
Code: ags
  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!   :-\

Trent R

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
To give back to the AGS community, I can get you free, full versions of commercial software. Recently, Paint Shop Pro X, and eXPert PDF Pro 6. Please PM me for details.


Current Project: The Wanderer
On Hold: Hero of the Rune

Khris

I've tried decrypting the first six lines of code ;)

Code: ags
  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).

Code: ags
  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.

.M.M.

Oh, thanks, this works great!  :) It ends like that when I´m copying code from my older games with just a minor changes...  ;)

NsMn

It's from KhrisMUC, it's almost unnessecary to tell HIM that it's working great  :P

.M.M.


SMF spam blocked by CleanTalk