Health bar scaling problem (SOLVED)

To deplete a health bar, i’m using:

function update(self, dt)
go.animate("#sprite", “scale.x”, go.PLAYBACK_ONCE_FORWARD, boss_health/250, go.EASING_LINEAR, 0)
end

The health bar works fine for 95% of the way, but at the final stretch, it seems to suddenly become partially transparent and it stretches in the y direction. Why is this?

Not sure why it would stretch along Y, but blurry/transparent is likely because of the linear texture filtering. You can replace it with nearest filtering.

Ideally you would want a simple shader, that based on an external param would display only a portion of the surface. I’ve made one for StarClick

1 Like

Here is what happens (ignore the background change):

image

image

image

image

2 Likes

If it’s a part of your user interface, why don’t you want to try to make it in gui? There is a clipping possibility (or you can use your approach as well, but even with 9 sliced images) and it is very convenient to make health bars :wink:

And go.animate() shouldn’t be called inside update() function, it should be called only once when the health value is changed, but in your example you are calling it on every frame. Somehow that can mess things up.
Move it into on_message() function for instance and do msg.post() when a hit happens.

I tried this and it yielded the same results. I think this is a glitch…

Can you provide a small sample project with just the health bar which shows the problem?

I have no clue how to share projects, so here is the health bar, centred on one side, so you can replicate the problem. First make the stuff from linear to nearest in graphics. Scale it manually in the x direction, maybe around 0.01 and see what happens when it is run.

healthbarfill

Zip the project folder, but don’t include the build or .git folders.

Testing.zip (17.1 KB)

Compare main.collection and the screen when it is run. Compare the game object scaled 1 and 0.01 in the x direction. You may have to squint.

1 Like

Yes, I see it as well. The solution in your case is to use a custom texture profile and disable mipmaps : https://defold.com/manuals/texture-profiles/#profiles

Two other solutions:

  1. Use a GUI box node for the health bar.
  2. Have the health bar graphics as a 1 pixel wide image and scale UP instead of down as you do now.
2 Likes

Instead of stretching a texture another option is to clip the XP bar by another node. Then you can have the clipped texture have special detail over it as well. There should be an example in this project

Another thing we commonly do in our games is to use 9patch for progress bars which repeat horizontally. You set a texture to 9 patch and then only set the first value to be whatever the width of the small base texture is. Then you can size that node to however wide you need and it will look nice.

2 Likes

I scaled it upwards since that seemed like the easiest option and it worked beautifully. Thanks everyone, though I still don’t know why it behaved like it did.

1 Like