GMS 2 Need help with "heart containers" in a zigzag pattern.

B

BanCannon

Guest
I want to have a sort of heart container system where there is an initial sprite that is worth more than 1 health, but any that are added to it are only worth one. Thanks to a couple different tutorials I have a working system in place, but I want it to look better and take up less room.

Here is my code in the Stats object:

Code:
Create Event:

/// @description Initialize Stats
hp = 4;
maxHp = 4;
extraHp = 4;
extraMaxhp = 4;



Step Event:

/// @description Execute Code\n
if keyboard_check_pressed(vk_down){
    if extraHp < 1 {
    hp --
    }
    else{
    extraHp --
        }
}

if keyboard_check_pressed(vk_up) {
    if hp != maxHp {
    hp ++
    }
    else{
    extraHp ++
    }
}

hp = clamp(hp,0,maxHp);
extraHp = clamp(extraHp,0,extraMaxhp);

if keyboard_check_pressed(vk_left) {
    extraMaxhp --
}

if keyboard_check_pressed(vk_right) {
    extraMaxhp ++
}

extraMaxhp = clamp(extraMaxhp,0,20)


Draw GUI Event:

/// @description Draw Hearts
    

draw_sprite(spr_heart,hp,xstart,ystart );


    
for (var i = 0; i < extraMaxhp; i++){
    draw_sprite(spr_extrahp_empty, 0, xstart+65+(28*i),ystart+40*(extraMaxhp%2))
    }

    
for (var i = 0; i < extraHp; i++){
    draw_sprite(spr_extrahp_full, 0, xstart+65+(28*i),ystart+40)
}
As of right now this means that instead of making a zigzag pattern like I want, it makes all of the sprites change y position together. I'm pretty new to all of this, so any help is appreciated.
 

Attachments

TheouAegis

Member
Because you're setting y based on extraMaxhp, not based on i%2 or i&1.

edit: i div 2 won't work here
 
Last edited:
B

BanCannon

Guest
Because you're setting y based on extraMaxhp, not based on i%2, i&1, or i div 2.
Thanks for the reply!
I tried doing this instead:
Code:
/// @description Draw Hearts
   

draw_sprite(spr_heart,hp,xstart,ystart );


   
for (var i = 0; i < extraMaxhp; i++){
    draw_sprite(spr_extrahp_empty, 0, xstart+65+(28*i),ystart+40*(i%2))
    }

   
for (var i = 0; i < extraHp; i++){
    draw_sprite(spr_extrahp_full, 0, xstart+65+(28*i),ystart+40*(i%2))
}
But the outcome is the same: whenever 1 health is added, all the "containers" switch y values at the same time. I need them to alternate.

In fact, the full containers don't move at all now for whatever reason.
 

TheouAegis

Member
Are you sure you don't have conflicting old code somewhere? Because your code is fine from what I can see. The primary tank is drawn at xstart,ystart; the first extra tank is at xstart+93,ystart; the second extra tank is at xstart+121,ystart+40; the 3rd tank is at xstart+149,ystart.

Looking at the numbers, I think you want ystart+105-40*(i%2)
 
Last edited:
B

BanCannon

Guest
Are you sure you don't have conflicting old code somewhere? Because your code is fine from what I can see. The primary tank is drawn at xstart,ystart; the first extra tank is at xstart+93,ystart; the second extra tank is at xstart+121,ystart+40; the 3rd tank is at xstart+149,ystart.

Looking at the numbers, I think you want ystart+105-40*(i%2)
Okay, so I was messing with the code and it turns out that Game Maker Studio just wasn't accepting my changes (I deleted a bunch of code to test this and nothing changed when I launched the game). I restarted Game Maker and everything worked exactly how I wanted it to. Thank you very much!
 
Top