1. Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice
  2. NOTICE: We will be applying a Xenforo update on Tuesday 25th of February. This means that from approximately 10:00 to 14:00 BST the forums will be offline (or possibly longer). Sorry for the inconvenience! Official Announcement here.

Timer for flashlight battery

Discussion in 'Programming' started by Luke Pierson, Sep 30, 2016.

  1. Luke Pierson

    Luke Pierson Member

    Joined:
    Sep 12, 2016
    Posts:
    143
    Hey everyone! Here again with another question :)

    So I have a flashlight item in my game that creates a timer that draws a battery sprite in the GUI. It should draw a different sprite as the timer goes down. It currently draws the full battery and then disappears as the timer goes to the battery_half mark and no new battery is drawn. Not sure what I'm doing wrong here, so I've come for some help.

    Code:
    myTime3=60.0;
    showTime3=ceil(myTime3);
    flashlightempty = false;

    if room != rm_win && obj_hero.flashlighton = true {
    if myTime3 > 0 {
    myTime3 = myTime3-delta_time/1000000;
    }
    else {
    myTime3 = 0;
    }
    showTime3=ceil(myTime3);
    }

    if (room != rm_win) && (obj_hero.flashlighton = true) && (showTime3 >= 41.0) {
    draw_set_halign(fa_left);
    draw_set_valign(fa_top);
    draw_sprite(spr_battery_full, 600, 100, 130);
    }
    else {
    if (room != rm_win) && (obj_hero.flashlighton = true) && ((showTime3 >= 21.0) && (showTime3 < 41.0)) {
    draw_set_halign(fa_left);
    draw_set_valign(fa_top);
    draw_sprite(spr_battery_half, 600, x-100, y-130);
    }
    else {
    if (room != rm_win) && (obj_hero.flashlighton = true) && ((showTime3 >= 1.0) && (showTime3 < 21.0)) {
    draw_set_halign(fa_left);
    draw_set_valign(fa_top);
    draw_sprite(spr_battery_near_empty, 600, x-100, y-130);
    }
    else {
    if (room != rm_win) && (obj_hero.flashlighton = true) && (showTime3 = 0) {
    draw_set_halign(fa_left);
    draw_set_valign(fa_top);
    draw_sprite(spr_battery_empty, 600, x-100, y-130);
    flashlightempty = true;
    }
    }
    }
    }

    How the system works is if the player uses the flashlight (indicated by the flashlighton variable), the timer is created and ticks down if flashlighton is true, and the seconds would be the showTime3 variable.
     
  2. Roderick

    Roderick Member

    Joined:
    Jun 22, 2016
    Posts:
    580
    That's a lot of code for a relatively simple task. Are you familiar with the draw_healthbar function? It might make things a lot simpler. Just draw an empty battery sprite, and put a healthbar inside it.
     
  3. Luke Pierson

    Luke Pierson Member

    Joined:
    Sep 12, 2016
    Posts:
    143
    I was not until now! Thank you! Is there a way to get the health bar color to be rgb by any chance? For more color customization? @Roderick
     
  4. Aura

    Aura Guest

    http://docs.yoyogames.com/source/da...wing/colour and blending/make_colour_rgb.html

    Note that colours in GM are BGR, not RGB.
     
    Luke Pierson likes this.

Share This Page