Resizing an image but keeping it aligned on the same x-position?

I have an HP bar. It needs to decrease in width, so I use xScale.

However, it shrinks about the middle. I’ve tried everything, from setting the reference point to left, to doing a lot of math to understand what is going on.

One of the primary issues I’m having is that I can’t get the value of the left edge of the image using image.x - image.width/2, given image.x is the center.

Image.x and image.width does NOT change after an xScale. So I have no idea of using math to make a workaround.

Hi,

try to set the width directly and do not use xScale.

[lua]

local new_width = 20

hp_bar.width = new_width
hp_bar.x = 100+new_width*0.5

[/lua]

That’s what I use in my actual project and it works fine.

Well this is what I’m doing now, but the bar is still moving, not staying on one spot.

self.hpValue = self.hpValue - damage

local newWidth = self.hp.width * self.hpValue/self.fullhpValue

self.hp.x = 100 + newWidth*0.5 – let’s just say I want to keep the hp bar’s leftmost point at x = 100

I’ve also tried taking the before and after width and using it to modify the x, but to no avail, even when I rounded all the numbers.

That’s not what I posted.

Do not use self.hp.width to estimate the new width, cause the hp width will change.

Please try the following, I tried to adjust it to your needs.

[lua]

–save the max and min width for the hp bar

self.hp.max_width = 100

self.hp.min_width = 0

local factor = self.hpValue/self.fullhpValue

local newWidth = self.hp.max_width * factor + self.hp.min_width

self.hp.width = newWidth

self.hp.x = 100 + newWidth*0.5

[/lua]

If this isn’t working for you, you have to post some code, cause I use exactly this method and it IS working.

@graytse89, the strategy of “image.x - image.width/2” in your first post should work if instead you use the contentWidth property like this: “image.x - image.contentWidth/2”.

  • Andrew

Thanks, that sort of fixed it for me (my hp is in a trapezoid form so shrinking it still causes the shape to deform, but this fixes most of it), I should have looked more into the APIs.

Hi,

try to set the width directly and do not use xScale.

[lua]

local new_width = 20

hp_bar.width = new_width
hp_bar.x = 100+new_width*0.5

[/lua]

That’s what I use in my actual project and it works fine.

Well this is what I’m doing now, but the bar is still moving, not staying on one spot.

self.hpValue = self.hpValue - damage

local newWidth = self.hp.width * self.hpValue/self.fullhpValue

self.hp.x = 100 + newWidth*0.5 – let’s just say I want to keep the hp bar’s leftmost point at x = 100

I’ve also tried taking the before and after width and using it to modify the x, but to no avail, even when I rounded all the numbers.

That’s not what I posted.

Do not use self.hp.width to estimate the new width, cause the hp width will change.

Please try the following, I tried to adjust it to your needs.

[lua]

–save the max and min width for the hp bar

self.hp.max_width = 100

self.hp.min_width = 0

local factor = self.hpValue/self.fullhpValue

local newWidth = self.hp.max_width * factor + self.hp.min_width

self.hp.width = newWidth

self.hp.x = 100 + newWidth*0.5

[/lua]

If this isn’t working for you, you have to post some code, cause I use exactly this method and it IS working.

@graytse89, the strategy of “image.x - image.width/2” in your first post should work if instead you use the contentWidth property like this: “image.x - image.contentWidth/2”.

  • Andrew

Thanks, that sort of fixed it for me (my hp is in a trapezoid form so shrinking it still causes the shape to deform, but this fixes most of it), I should have looked more into the APIs.