GMS 2 help healtbar flash

Discussion in 'Programming' started by diester, Sep 13, 2019.

  1. diester

    diester Member

    Joined:
    Sep 5, 2019
    Posts:
    24
    Good evening, I still need your knowledge.

    I created a life bar and would like to have it flashing at a certain percentage.
    what I did, a bar life with the value 40 (which is the max of life).
    I then try to apply a flash when the bar reaches 10 of life.

    the problem is that it flashing when it is 40 ( max life) :confused:.

    here is the main code to make it flashing:

    creat :
    Code:
    draw_health_ = 0;
    
    flash = 0;
    
    
    step :

    Code:
    if (!variable_instance_exists(id, "max_health_")) max_health_ = 40;
    max_health_ =- real(10);
    
    alarm[1] = game_get_speed(gamespeed_fps);
        if(flash = 0)
    {
        flash = 1;
        alarm[2] = room_speed/5;
        image_alpha = 0.5;
    }
    alerte 2 :

    Code:
    image_alpha = 1;
    alarm[3] = room_speed/5;
    alerte 3 :

    Code:
    flash = 0;
    draw :

    Code:
    draw_self();
    
    if !instance_exists(obj_player) exit;
    
    draw_health_ = lerp(draw_health_, obj_player.health_, .20);
    
    
    col = merge_color($FF0000FF,$FFFFFF, 0.5)
    
    
        draw_set_color(col);
       
    draw_rectangle( x+4, y+4, x+126*draw_health_/obj_player.max_health_, y+11, false) ;
    sorry for my english i use translator
     
  2. CedSharp

    CedSharp Member

    Joined:
    Sep 5, 2016
    Posts:
    664
    Your condition for flashing is "if( flash = 0 )", you don't test the health...
    You probably want something like "if( flash = 0 && health <= 10 )"

    Hope this helps ~
    Cedrik
     
  3. diester

    diester Member

    Joined:
    Sep 5, 2019
    Posts:
    24
    I will test but if I put that it does not blink at all neither at the beginning nor at 10 of life

    Code:
    if (!variable_instance_exists(id, "max_health_")) max_health_ = 40;
    max_health_ =- real(10);
    
    alarm[1] = game_get_speed(gamespeed_fps);
        if ( flash = 0 && health <= 10 )   " i have try : max_health_ and health_ " dont run
    {
        flash = 1;
        alarm[2] = room_speed/5;
        image_alpha = 0.5;
    }
    that's why I do not understand that it is flashing with the bar at max and not a 10 of life oO

    in obj_player ( creat)

    Code:
    spd_bounce = 0;
    max_health_ = 40;
    health_ = max_health_;
     
    Last edited: Sep 13, 2019
  4. Bentley

    Bentley Member

    Joined:
    Jun 18, 2017
    Posts:
    790
    max_health_ =- real(10);
    That seems weird. Are you trying to subtract 10 from max_health? You would just use max_health_ -= 10.
     
  5. diester

    diester Member

    Joined:
    Sep 5, 2019
    Posts:
    24
    not in fact this line meant that if life is 10 or less, it launches the flash
     
    Last edited: Sep 14, 2019
  6. CedSharp

    CedSharp Member

    Joined:
    Sep 5, 2016
    Posts:
    664
    To make a "do this if something" you normally use a "if()" statement.
    Here is how your code should probably look:

    Code:
    // Create Event
    
    max_health = 40;
    draw_health = max_health;
    
    flashing = false;
    flash_time = room_speed / 5;
    health_visible = true;
    
    Code:
    // Step Event
    
    // you can use obj_player.health_,
    // but health is a global variable,
    // check the documentation
    if(!flashing && health <= 10) {
      flashing = true;
      health_visible = false;
      alarm[0] = flash_time;
    }
    else if(flashing && health > 10) {
      flashing = false;
      alarm[0] = 0; // Stop the alarm
      health_visible = true;
    }
    Code:
    // Alarm 0
    health_visible = !health_visible;
    if(flashing) {
      alarm[0] = flash_time;
    }
    Code:
    // Draw Event
    draw_self();
    
    if(health_visible && instance_exists(obj_player)) {
      draw_health = lerp(draw_health, obj_player.health_, .20);
      // BBGGRR, 8 is half of 16 (0 - F = 16 values)
      draw_set_color($8888FF); // same as your merge color code, but without running scripts!
      draw_rectangle( x+4, y+4, x+126*draw_health/max_health, y+11, false) ;
    }
    I didn't test this code, but with this you should have a nice Idea of how to write the code :p
    ~ Hope this helps
     
  7. diester

    diester Member

    Joined:
    Sep 5, 2019
    Posts:
    24
    not always not ...... I do not understand what's wrong. it is not so complicated. that torments me XD
     
  8. Joe Ellis

    Joe Ellis Member

    Joined:
    Aug 30, 2016
    Posts:
    913
    It's cus of this part in the step event:

    if(flash = 0)
    {
    flash = 1;
    alarm[2] = room_speed/5;
    image_alpha = 0.5;
    }

    So it makes it start flashing straight away, you need to add another condition before that: if (health_ <= 10) && (flash = 0)
     
  9. diester

    diester Member

    Joined:
    Sep 5, 2019
    Posts:
    24
    it still does not work it drives me crazy
     
  10. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,470
    You just need to set the alarm only when the health transitions from above 10 to not, and all that takes is a was-is flag.

    Read this: Restricting Unwanted Repetition with Flags (was-is flags)

    Create:
    Code:
    was_low = false;
    is_low = false;
    
    draw_health_ = 0;
    health_visible = true;
    
    Step:
    Code:
    is_low = obj_player.health_ <= 10;
    if (was_low ^^ is_low) {
      if (is_low) { // From above 10 to lower
        alarm[0] = room_speed/5;
        health_visible = false;
      } else { // From 10 or below to higher
        alarm[0] = -1;
        health_visible = true;
      }
    }
    was_low = is_low;
    
    Alarm 0:
    Code:
    health_visible = !health_visible;
    alarm[0] = room_speed/5;
    Draw:
    Code:
    draw_self();
    draw_health_ = (abs(draw_health_-obj_player.health_) < 1) ? obj_player.health_ : lerp(draw_health_, obj_player.health_, 0.2);
    if (health_visible && instance_exists(obj_player)) {
      draw_set_color($8888FF);
      draw_rectangle(x+4, y+4, x+126*draw_health_/obj_player.max_health_, y+11, false) ;
    }
    
     
    diester likes this.
  11. diester

    diester Member

    Joined:
    Sep 5, 2019
    Posts:
    24
    I still do not understand why the codes that should have worked did not work ... I'm going to search because I like to understand.

    this said thank you this code works in addition I just learned to use this feature that I did not know and that can be useful in many cases. Thank you FrostyCat for the code but also the function I did not know. :D
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice