GameMaker: Studio

GameMaker: Studio

View Stats:
Smakdude Apr 23, 2014 @ 1:28pm
how do i make a battery meater that gose down every time you move
I am making a game where my caricter is a robot that can shift its gravity and I wont a bar tha ever time you move it gos down like a battry kindathing but I dont know how to do it please help
< >
Showing 1-3 of 3 comments
Zaron X Apr 23, 2014 @ 2:10pm 
Step 1 would be having a variable for your energy; pop one into the Create event to declare it, set an appropriate value, and that's as good as done. Draining the energy is just a matter of subtracting to that variable at any appropriate time; if it's just when gravity shifts, subtract it during that event, whereas if it's a constant drain while moving, that's where you'll want to put it.

As for drawing the meter, the first step (to make sure things are working how you want) would likely be to just use draw_text() or the drag and drop equivalent to draw the value of your energy variable somewhere on the screen. You'd do this just so you can track it easily and make sure it's going down when you want it to. (You can also do this in Debug, but GM's Debug tool has its own learning curve and input needs, so even if you know how to track it in there it's often easier to just draw the value on the screen.) If the numbers are behaving how you want, you're on to drawing a meter.

Meters tend to work with percentages a lot, meaning the most important factor will be your actual energy value divided by the maximum. If you want something to visually behave like a battery meter (in bars), you'll want to either do a little rounding or look into the mod function, which is a selectively handy math tool to have when the need arises. For a simpler, straight-forward long bar meter, the width of the bar is just going to be how long it is at maximum times the percentage it's at now. This works for sprite scaling and rectangle drawing alike, so it's super handy to know.
Sete Apr 23, 2014 @ 2:28pm 
if you already have done a movement system, just add a line in the end of the code, to subtract energy from battery. (use global variables to do this)
in the same object that you are using to move your caracter;

global.battery = 100 //set inicial state

(your movement event, probably a key event);
if (global.battery > 0) // only reduces your battery if you have it. so you dont get negative // values.
global.battery = global.battery - x // "x" is the value of decrease

//or global.battery -= x
//this will reduce 'x' from your battery every time you move.

text after // is a comment not a code.

Last edited by Sete; Apr 23, 2014 @ 2:31pm
Festive Grim Apr 29, 2014 @ 7:50pm 
draw_healthbar(x-16,y-16,x+16,y-24,energy,c_black,c_red,c_green,1,1,0) I think, I did all the arguments off the top of my head, so you would have to redo the last few.
< >
Showing 1-3 of 3 comments
Per page: 15 30 50

Date Posted: Apr 23, 2014 @ 1:28pm
Posts: 3