GameMaker need help with health bar

O

OriginalGrim

Guest
I want to make it so that as hp goes down the health bar gets shorter but when I tried to make that happen it reduced the image from the left and right into the center. Is there a way to anchor the left side of this sprite so the it will reduce from right to left.
 

Attachments

P

Pyxus

Guest
I want to make it so that as hp goes down the health bar gets shorter but when I tried to make that happen it reduced the image from the left and right into the center. Is there a way to anchor the left side of this sprite so the it will reduce from right to left.
There are countless ways to visually display that health is going down, not exactly sure how to help you unless you give some insight into what you're doing.
 

Simon Gust

Member
Make sure the origin of the sprite is to the left. You can do that in the sprite editor.
A tab called "Origin", you can put it to 0 and 0.
 
O

OriginalGrim

Guest
There are countless ways to visually display that health is going down, not exactly sure how to help you unless you give some insight into what you're doing.
create event
Code:
maxhp = 100
crnthp = 100
step event
Code:
if keyboard_check(vk_space){
crnthp -= 1;
}
draw
Code:
draw_sprite_ext(spr_healthbar, 0, x, y, crnthp/100, 1, 0, c_white, 1)
this is what I'm doing right now.
 
O

OriginalGrim

Guest
Make sure the origin of the sprite is to the left. You can do that in the sprite editor.
A tab called "Origin", you can put it to 0 and 0.
I'll test that real quick and see if that work it would be nice if the solution is this simple :)
 
O

OriginalGrim

Guest
Make sure the origin of the sprite is to the left. You can do that in the sprite editor.
A tab called "Origin", you can put it to 0 and 0.
that fixed part of the problem as it no longer goes to the center however it is still stretching the sprite in response to hp change rather that earasing the sprite from right to left as hp goes down.
 
P

Pyxus

Guest
I'll test that real quick and see if that work it would be nice if the solution is this simple :)
Ah, ok. Since you're trying to do this by decreasing the image x scale then what @Simon Gust said is exactly what you're looking for however it will cause the sprite to sort of scrunch. Instead why not use the draw_sprite_part function? Assuming the crunch effect isn't something you intended. Here is how the two will look, the sprite on top changes the image x, the sprite below changes what part is drawn.
https://i.imgur.com/SewX2YR.gifv
Code:
draw_sprite_part(spr_healthbar, 0, 0, 0, (hp/100)*sprite_width, sprite_height, x, y+40);
 
O

OriginalGrim

Guest
Ah, ok. Since you're trying to do this by decreasing the image x scale then what @Simon Gust said is exactly what you're looking for however it will cause the sprite to sort of scrunch. Instead why not use the draw_sprite_part function? Assuming the crunch effect isn't something you intended. Here is how the two will look, the sprite on top changes the image x, the sprite below changes what part is drawn.
https://i.imgur.com/SewX2YR.gifv
Code:
draw_sprite_part(spr_healthbar, 0, 0, 0, (hp/100)*sprite_width, sprite_height, x, y+40);
this is exactly what I have written but the sprite won't show up at all and I don't know why.

Code:
Cstats[0,0] = 100; (max hp)
CRhp = Cstats[0,0];
Code:
if keyboard_check(vk_space){
    CRhp -= 1;
}

if keyboard_check_pressed(ord("A")){
    CRhp += 25;
}

CRhp = clamp(CRhp,0,Cstats[0,0]);
Code:
draw_sprite_part(sprite2, 0, 0, 0, (CRhp/100)*sprite_width, sprite_height, x, y);
 
Last edited by a moderator:
P

Pyxus

Guest
this is exactly what I have written but the sprite won't show up at all and I don't know why.

Code:
Cstats[0,0] = 100; (max hp)
CRhp = Cstats[0,0];
Code:
if keyboard_check(vk_space){
    CRhp -= 1;
}

if keyboard_check_pressed(ord("A")){
    CRhp[0,0] += 25;
}

CRhp = clamp(CRhp,0,Cstats[0,0]);
Code:
draw_sprite_part(sprite2, 0, 0, 0, (CRhp/100)*sprite_width, sprite_height, x, y);
Try this:
Code:
var s_width = sprite_get_width(spr_healthbar);
var s_height = sprite_get_height(spr_healthbar);
draw_sprite_part(spr_healthbar, 0, 0, 0, (hp/100)*s_width, s_height, x, y+40);
 
O

OriginalGrim

Guest
Try this:
Code:
var s_width = sprite_get_width(spr_healthbar);
var s_height = sprite_get_height(spr_healthbar);
draw_sprite_part(spr_healthbar, 0, 0, 0, (hp/100)*s_width, s_height, x, y+40);
That worked thank you so much for the help I guess sprite_width and height weren't getting the x and y of the sprite :)
 
Top