[SOLVED] How to create a hitpoint bar with sprites (no GUI)

Hi all

As mentioned in the title, I’d like to create hitpoint bars but not in GUI (directly in the game as game objects/sprites etc.)

But…

1/ Maybe things have changed, but looks like it’s not possible in Defold to mask/clipping sprites in Defold… (would have been the most convenient solution)

2/ I suppose I could resize a 1-pixel sprite, but I don’t know how to" left-align" the pivot point of a 2D sprite :thinking:


=> in this video, the bar is randomly scaled every sec, but the pivot point is centered, so…

How should I proceed? Any advice?

1 Like

You need to put the sprite in a game object, shift the sprite’s position within the GO by its full length to the right and set the game object’s position by the same amount to the left (so the sprite is centered while full). Then change the GO’s scale.x to set the bar’s size.

That’s assuming the sprite for the bar is full-size. For a 1-pixel sprite you’d probably want to shift its position by 0.5 and go from there.

3 Likes

Quick and easy would be to have 100 one-pixel sprites and activate/deactivate them :grinning:

1 Like

I used this solution and it works fine :slight_smile: Thanks

It “forces” me to have other game objects for the other XP bar elements (background, foreground frame etc.) but at least the bar itself works as intended.

I considered this solution (when I was desperate :grin:) that but wouldn’t it cause performance issues? :thinking:

Anyway, it’s pretty weird that it’s not possible to mask/clipping sprites (there are many other contexts where it could be useful).

But thanks both of you!

1 Like

I think it’s easy to fall into the trap of “wouldn’t that cause performance issues?” (because I do it all the time) but realistically, 100 sprites is not worth worrying about, as long as they are coded efficiently.

In any case, now that I think about it, the easiest thing would be to offset the X value according to the scale. You’re already calculating scale, you can calculate the position based off that very easily.

Edit: Which i now realise is what Klear suggested. You mentioned that it “forces” you to have other game objects, but remember that GOs have very very little effect on performance and should be used liberally without concern.

2 Likes