Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

HP Bar-related question

Hey all, um, quick novice question to ask. I'm trying to change a script for an HP bar so that it depletes vertically rather than horizontally and I'm having some trouble (mostly because I'm just playing around with it without any real understanding of what I'm doing)

Code:
  #--------------------------------------------------------------------------

  # * Refresh

  #--------------------------------------------------------------------------

  def refresh(actor)

    hppercent = actor.hp.to_f / actor.maxhp.to_f

    if hppercent > 0.66

      @sprites[1].bitmap = RPG::Cache.picture($game_set.hpbar_good)

    elsif hppercent > 0.33

      @sprites[1].bitmap = RPG::Cache.picture($game_set.hpbar_okay)

    else

      @sprites[1].bitmap = RPG::Cache.picture($game_set.hpbar_bad)

    end

    bar_width = (@sprites[1].bitmap.width.to_f * hppercent).round

    w = @sprites[1].bitmap.width-bar_width

    @sprites[1].src_rect = Rect.new(0, 0, bar_width, @sprites[1].bitmap.height)

  end

My solution was to change everything that related to 'width' and replace it with 'height' but all that achieved was a HP bar that doesn't change. Any ideas guys?
 
As far as I can see from a quick glance the shape of the HP Bar is controlled by the pictures you use. For example, when your HP is above 66% the HP bar picture shows the picture in the pictures file that is set to whatever you put as hpbar_good. I'm not too sure about the configurations of your script, but basically, all you have to do is change the pictures being used.
 
Thanks for looking in at it Scripter, but that determines the colour of the HP bar being used (Green for okay, Yellow is danger... etc). I'm sure that this

Code:
bar_width = (@sprites[1].bitmap.width.to_f * hppercent).round

    w = @sprites[1].bitmap.width-bar_width

    @sprites[1].src_rect = Rect.new(0, 0, bar_width, @sprites[1].bitmap.height)

controls the direction which it depletes in, but I don't understand why it wouldn't work if I changed all the 'width' to 'height'. Did that make it any easier to understand? I'm not so great at getting a point across sometimes...^^;


Edit: Oh, never mind. I've solved the problem by fluke... Can I get a lock on this topic please?
 
In case someone else peaks in for the solution....

the 'src_rect' in a blt command (which will get used in another method to draw the bar) is the portion of the source bitmap that gets drawn into the destination bitmap. The format of the Rect.new statement is:

Rect.new(x_origin, y_origin, x_width, y_height)

So, you should change the section above to:

Code:
bar_height = (@sprites[1].bitmap.heihgt.to_f * hppercent).round

    h = @sprites[1].bitmap.height-bar_height

    @sprites[1].src_rect = Rect.new(0, 0, @sprites[1].bitmap.width, bar_height)

h probably gets used to position the src_rect in the blt statement.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top