GMS 2 help fitting boxes

kaptenr

Member
Hello, I am beginner in gms. And I am pondering on how to stack boxes within a space (the red space in the pictures). I want the boxes to be stacked automatically as I add them, and they can be any height or width.
I want the boxes to be as close to center as possible, as shown in this picture:
boxes_fitting_1box.png

If I add another box, the total width of the 2 boxes is still centered, as in this picture:
boxes_fitting_2boxes.png

If I add a 3rd box, and it won't fit in the red space, it jumps up and is as close as possible to the highest box, like in this picture:
boxes_fitting_3boxes.png

So far so good, I manage to get this to work. But I realized a problem. I checked this manually, with if statements for everything, but this approach becomes really hard now, there is so many scenarios to check for. What if it looks like in this picture:
boxes_fitting_6boxes.png

I think I need another approach, how would you program this? Does anyone have any good idea or tips on how to tackle this problem?
 

samspade

Member
While slightly different than what you're talking about, this video provides a fairly good example of the logic you would use. The language is different but the concepts are the same.

 

kaptenr

Member
While slightly different than what you're talking about, this video provides a fairly good example of the logic you would use. The language is different but the concepts are the same.
Thanks for answer!! :) Yes that video is kinda how I want it to work. But I don't want the boxes to grow like that. I first decide the width and height of the box, and then add it, and I want it to find it's place. But the video gave me some ideas, I should probably use collision somehow. I will think about it :)
 
Top