• 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 trouble implementing a growing rectangle

F

fxokz

Guest
So basically the effect I'm trying to achieve is: when the player presses "I" a 0x0 rectangle is created in the center of the view that quickly grows to cover a about 2/3rds of the screen so that it can then display the players inventory. Its supposed to be a subtle effect to make the game look more smooth but I just cant seem to implement it. The rectangle either grows somewhere I don't intend for it to grow or doesn't grow the right width and height. its a mess..

I really didn't think I would struggle with something this simple.. heres the code I use:

create event:
Code:
//obj_inventory coordinates also in step event
x = view_xview + (view_wview/2);
y = view_yview + (view_hview/2);

show_inv = 0; //boolean

//starting rect
rec_startx1 = x;
rec_starty1 = y;
rec_startx2 = x;
rec_starty2 = y;

rec_width = view_wview/2;
rec_height = view_hview/2;

//ending rect
x1 = x - rec_width/2;
x2 = x + rec_width/2;
y1 = y - rec_height/2;
y2 = y + rec_height/2;
draw event:
Code:
if (show_inv)
{
    draw_set_colour(c_black);
    draw_set_alpha(1);
    draw_roundrect(rec_startx1, rec_starty1, rec_startx2, rec_starty2, 0);
    if (rec_startx1 > view_xview + x1) rec_startx1 --;
    if (rec_startx2 < view_xview + x2) rec_startx2 ++;
    if (rec_starty1 > view_yview + y1) rec_starty1 --;
    if (rec_starty2 < view_yview + y2) rec_starty2 ++;               
}   else {
    rec_startx1 = x;
    rec_starty1 = y;
    rec_startx2 = x;
    rec_starty2 = y;
}
I pressed event:
Code:
if (show_inv == 0)
{
    show_inv = 1;
}   else if (show_inv == 1)
{
    show_inv = false;
}
I feel as if I overcomplicated things in this attempt, is there anything that could have made this much easier?
 

Simon Gust

Member
So basically the effect I'm trying to achieve is: when the player presses "I" a 0x0 rectangle is created in the center of the view that quickly grows to cover a about 2/3rds of the screen so that it can then display the players inventory. Its supposed to be a subtle effect to make the game look more smooth but I just cant seem to implement it. The rectangle either grows somewhere I don't intend for it to grow or doesn't grow the right width and height. its a mess..

I really didn't think I would struggle with something this simple.. heres the code I use:

create event:
Code:
//obj_inventory coordinates also in step event
x = view_xview + (view_wview/2);
y = view_yview + (view_hview/2);

show_inv = 0; //boolean

//starting rect
rec_startx1 = x;
rec_starty1 = y;
rec_startx2 = x;
rec_starty2 = y;

rec_width = view_wview/2;
rec_height = view_hview/2;

//ending rect
x1 = x - rec_width/2;
x2 = x + rec_width/2;
y1 = y - rec_height/2;
y2 = y + rec_height/2;
draw event:
Code:
if (show_inv)
{
    draw_set_colour(c_black);
    draw_set_alpha(1);
    draw_roundrect(rec_startx1, rec_starty1, rec_startx2, rec_starty2, 0);
    if (rec_startx1 > view_xview + x1) rec_startx1 --;
    if (rec_startx2 < view_xview + x2) rec_startx2 ++;
    if (rec_starty1 > view_yview + y1) rec_starty1 --;
    if (rec_starty2 < view_yview + y2) rec_starty2 ++;              
}   else {
    rec_startx1 = x;
    rec_starty1 = y;
    rec_startx2 = x;
    rec_starty2 = y;
}
I pressed event:
Code:
if (show_inv == 0)
{
    show_inv = 1;
}   else if (show_inv == 1)
{
    show_inv = false;
}
I feel as if I overcomplicated things in this attempt, is there anything that could have made this much easier?
Leave out all view_xview and view_yview variables and put the draw code into the draw gui event. that simplyfies things a lot. Then you don't have to worry about view position, the graphics are always on screen.
 
F

fxokz

Guest
Leave out all view_xview and view_yview variables and put the draw code into the draw gui event. that simplyfies things a lot. Then you don't have to worry about view position, the graphics are always on screen.
I tried that aswell and it still was a bit glitchy. Also how can i change a variable over a set period of time? For example from 20 to 1500 over 60 steps.
 

Simon Gust

Member
Hmm weird, the code works for me
Code:
//obj_inventory coordinates also in step event
xx = (view_wview/2);
yy = (view_hview/2);

show_inv = 0; //boolean

//starting rect
rec_startx1 = xx;
rec_starty1 = yy;
rec_startx2 = xx;
rec_starty2 = yy;

rec_width = view_wview/2;
rec_height = view_hview/2;

//ending rect
x1 = xx - rec_width/2;
x2 = xx + rec_width/2;
y1 = yy - rec_height/2;
y2 = yy + rec_height/2;
Code:
/// rec
if (show_inv)
{

    draw_roundrect(rec_startx1, rec_starty1, rec_startx2, rec_starty2, 0);
    if (rec_startx1 > x1) rec_startx1 --;
    if (rec_startx2 < x2) rec_startx2 ++;
    if (rec_starty1 > y1) rec_starty1 --;
    if (rec_starty2 < y2) rec_starty2 ++;              
}  
else 
{
    rec_startx1 = xx;
    rec_starty1 = yy;
    rec_startx2 = xx;
    rec_starty2 = yy;
}
Code:
if (keyboard_check_pressed(vk_space))
{
    show_inv = !show_inv;
}

to expand a distance over a certain time you have to first find the delta of x2 - x1 and y2 - y1. Then divide them by 60.
Code:
var xspd = (rec_width/2) / 60;
var yspd = (rec_height/2) / 60;
 
F

fxokz

Guest
Hmm weird, the code works for me
Code:
//obj_inventory coordinates also in step event
xx = (view_wview/2);
yy = (view_hview/2);

show_inv = 0; //boolean

//starting rect
rec_startx1 = xx;
rec_starty1 = yy;
rec_startx2 = xx;
rec_starty2 = yy;

rec_width = view_wview/2;
rec_height = view_hview/2;

//ending rect
x1 = xx - rec_width/2;
x2 = xx + rec_width/2;
y1 = yy - rec_height/2;
y2 = yy + rec_height/2;
Code:
/// rec
if (show_inv)
{

    draw_roundrect(rec_startx1, rec_starty1, rec_startx2, rec_starty2, 0);
    if (rec_startx1 > x1) rec_startx1 --;
    if (rec_startx2 < x2) rec_startx2 ++;
    if (rec_starty1 > y1) rec_starty1 --;
    if (rec_starty2 < y2) rec_starty2 ++;           
}
else
{
    rec_startx1 = xx;
    rec_starty1 = yy;
    rec_startx2 = xx;
    rec_starty2 = yy;
}
Code:
if (keyboard_check_pressed(vk_space))
{
    show_inv = !show_inv;
}

to expand a distance over a certain time you have to first find the delta of x2 - x1 and y2 - y1. Then divide them by 60.
Code:
var xspd = (rec_width/2) / 60;
var yspd = (rec_height/2) / 60;
I used the same exact code but instead of drawing the rectangle in the center of the screen, its a much smaller rectangle on the top left hand corner of the screen instead. not sure why its like that but I'm guessing it has to do with the view or something..

okay so I just realised I had to use:
Code:
window_get_width()

window_get_height()
instead of view_hview and view_wview.. sorry for my lack of patience before replying, and thanks for helping me.
 
Last edited:
Top