• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM making a bar with a greenzone

Fxokz

Member
so ive went about making a bar in which the player presses spacebar when the line is in the green zone.

Ive made it as simple as making a sprite which is the bar and greenzone and using it in an object which draws a 1 pixel line that moves back and forward followed by some hard set constants.

GML:
draw_self();

var sprWidth, sprHeight, max_x, min_x;
sprWidth = sprite_get_width(sprite_index);
sprHeight = sprite_get_height(sprite_index);
min_x = x;
max_x = (x + sprWidth);

draw_line(xx, y-1, xx, (y - 1) + sprHeight);

if (change != 1){
    if xx < max_x
    {
        xx += spd;
    } else change = 1;
}

if (change == 1) {
    if (xx > min_x)
    {
        xx -= spd;
    } else change = 0;
}

if keyboard_check_pressed(vk_up) && (canPress == true)
{
    if (xx > (x + 22) && (xx < x + sprite_width-22))
    {
        show_message("your charge worked");
    } else show_message("you missed your shot");
    instance_destroy();
}

it works. it works well but its disgusting.
Ideally id like to draw everything in code. The bar, a green bar within and make some things tweakable so that it can eventually be called from a script. Has anyone programmed something similar?
 

Luke Peña

Member
Instead of using actual x coordinates, you could just use a value that ping pongs from 0 to 1 to 0 and so on. Then you could have a green_min and green_max value that could be something like .7 and .8. When the player presses space bar or whatever we just ask if the position is greater than green_min and smaller than green_max. If so, do the thing.

To draw it, you can draw a box at whatever width you want, and to draw that line you can just draw the x position at box.x + (box_width * line_pos); Because line_pos is a value between 0 and 1, we can use that to determine where the line is in any width. This way, your box position, size, and charge speed or whatever is independent from the UI / visual representation.
 

Fxokz

Member
Instead of using actual x coordinates, you could just use a value that ping pongs from 0 to 1 to 0 and so on. Then you could have a green_min and green_max value that could be something like .7 and .8. When the player presses space bar or whatever we just ask if the position is greater than green_min and smaller than green_max. If so, do the thing.

To draw it, you can draw a box at whatever width you want, and to draw that line you can just draw the x position at box.x + (box_width * line_pos); Because line_pos is a value between 0 and 1, we can use that to determine where the line is in any width. This way, your box position, size, and charge speed or whatever is independent from the UI / visual representation.
Wow thats genius. Cant imagine tying a visual representation to the code. Ill give a crack at it.
 
Top