Object is Being redrawn every step, animation wont play.

K

KC Frye

Guest
My heath meter object (which is a zelda-style heart system) has an animation that causes it to blink red when the player character is hit, so as to attract the attention of the player and alert them that they've been struck.

However, because I've set it up in the Draw GUI event, the object gets redrawn every step of the game, therefore the animation doesn't appear to run because it's constantly being restarted at the first frame. What is the best solution to fix this?

Code:
var width, xx, yy, xempty, yempty, xfull, yfull, xhalf, yhalf, xquarter, yquarter, xthreequarter, ythreequarter;

width = display_get_gui_width();

//x buffer between bubbles
xx = width - 36 * 6; //36 = health width (32) + buffer (4) //6 = # of health bubbles per row

//y buffer between top of screen and bubbles
yy = 24;

xempty = 0;
yempty = 0;
xfull = 0;
yfull = 0;
xhalf = 0;
yhalf = 0;

obj_player.maxhitpoints = clamp(obj_player.maxhitpoints, 6, 48);
obj_player.hitpoints = clamp(obj_player.hitpoints, 0, obj_player.maxhitpoints);

//----------------------------------------------------------------------------

//draw text

draw_set_color(c_white);
draw_text(width - (36 * 6) / 2, 0, "[L I F E]");
draw_set_halign(fa_left);

//--------------------------------------------------------------------------

//EMPTY BUBBLES

repeat(obj_player.maxhitpoints/2) {
    if (xempty == 36 * 6) {
    yempty = 36;
    xempty = 0;
    }
    draw_sprite (spr_HealthEmpty, 7, xx + xempty, yy + yempty)
    xempty += 36;
}



//HALF BUBBLES
repeat(floor(obj_player.hitpoints/2) + frac(obj_player.hitpoints/2) * 2)
{
    if (xhalf >= 36 * 6) {
    yhalf = 36;
    xhalf = 0;
    }
    draw_sprite (spr_HealthHalf, 7, xx + xhalf, yy + yhalf)
    xhalf += 36;
}

//FULL BUBBLES
repeat(floor(obj_player.hitpoints/2))
{
    if (xfull >= 36 * 6) {
    yfull = 36;
    xfull = 0;
    }
    draw_sprite (spr_HealthFull, 0, xx + xfull, yy + yfull)
    xfull += 36;
}
    
    
//----------------------------------------------------
with (obj_player){
if (keyboard_check_pressed(vk_up)) { ++hitpoints}
if (keyboard_check_pressed(vk_down)) { --hitpoints}
if (keyboard_check_pressed(vk_left))  {maxhitpoints += 2}
if (keyboard_check_pressed(vk_right)) {maxhitpoints -= 2}   
}
 

mar_cuz

Member
The issue is in the image_index of the sprites. Your telling it to draw either index 0 or 7. If you put image_index there instead of the numbers it should play the animation but you will have to change how your whole system works because then you can't define specific image_indexs to draw.
 

TheouAegis

Member
Your flash timer div time between color changes mod frames of animation

So if you had a variable flash_timer and 4 seconds between colors and 6 frames, it would be

draw_sprite (spr_HealthFull, flash_timer div 4 mod 6, xx + xfull, yy + yfull)
 
Top