Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Health Bar: add/subtract size to the left or right?

xactoxacto Posts: 146Member
edited November -1 in Tech Support
I am creating a health bar (a red box PNG) and I want it to shrink from the left.

If I set up code to adjust the SIZE of a graphic (subtract width), it will reduce from the CENTER. Is there a way to adjust the center point so I am able to grow from left or right?

The only other way I can think about doing this is spawning multiple PNG boxes that would touch and I would destroy them when I need to reduce. (This seems like a lot of work)

Any other way around this?


  • Fafnir312Fafnir312 Posts: 161Member
    Hmmm... I don't know how to offset the center (if that's even possible) but you could shrink the actor and then offset it the same amount it shrinks. Then it'd look like it's staying in the same place and shrinking.
  • CodeMonkeyCodeMonkey Posts: 1,803Head Chef, Member, PRO
    First: Look at what is always constant. The right edge of the health bar. Lets say its at x=470.
    Second: Find the center of the actor. Its always half of the width.

    Now constrain the position of the actor relative to the right border and the width.

    Constrain Attribute: self.Position.X = 470 - self.Size.Width/2
  • xactoxacto Posts: 146Member

    Not sure if I am following you. My graphic I am using is a 8x8 white square that will. Are you saying I should constrain the X attribute to "4-self.size.width/2"?

    @tshirtbrooth, I tired but for some reason did not work - not sure why.

    Another note.... this is really a timer bar, the time may be different each time because of powerups.
  • ShanestaShanesta Posts: 63Member
    If you have a variable for Time
    You could just constrain self.player.X to 470-(game.Time/1)
  • CodeMonkeyCodeMonkey Posts: 1,803Head Chef, Member, PRO
    You can also try with the Replicate behavior.

    See both methods here:
  • xactoxacto Posts: 146Member

    Thanks for the code.... it now makes sense!!!!

    That needs to be in a wiki. :)
This discussion has been closed.