• 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 [SOLVED] Multiple Level Buttons

ZyKro

Member
I am trying to implement a level select to my platform game. I want to be able to create multiple buttons to select the level without having to individually place all of them and put in creation code to go to the level I want. I was wondering if I could use a for loop for it, but I have been having trouble with doing this.
I want to be able to easily create multiple buttons with a number telling the level it is and be able to go to the level without having to use creation code.
 

SoVes

Member
this should work if you use a mouse

in the buttons step
Code:
if (mouse_check_button_pressed(mb_left) && position_meeting(mouse_x, mouse_y, id)) {
   room_goto( room name);
}
 

ZyKro

Member
I also want to be able to create multiple buttons and not have to use creation code to go to rooms. Could I use a script and load into an object to create multiple instances with different numbers for the level number?
 

Simon Gust

Member
Loops and arrays are your friends in this one.
Code:
// create
/*
create an array that holds the room ids
*/
room_id[0] = room_first_level;
room_id[1] = room_second_level;
...

// draw
/*
then, you need to assign buttons to the array, check for their positions and draw them
*/
for (var i = 0; i < 10; i++)
{
  // assign positions
  var xx = 20 + i * 32;
  var yy = 20;
  var wdt = sprite_get_width(spr_room_buttons);
  var hgt = sprite_get_height(spr_room_buttons);

  // select
  if (point_in_rectangle(mouse_x, mouse_y, xx, yy, xx + wdt, yy + hgt))
  {
    if (mouse_check_button_pressed(mb_left))
    {
      room_goto(room_id[i]);
    }
    // if you want to make an effect that the mouse in hovering on the button, here would be it
    //draw_sprite(spr_button_highlight, 0, xx, yy);
  }

  // draw
  draw_sprite(spr_room_buttons, i, xx, yy);
}
This way you're not using instances at all, only sprites.
 

Simon Gust

Member
Is it only the highlight that isn't working and nothing else?
Because important is that the rest works first.
Do you get an error message?
 

ZyKro

Member
I did not get an error message, but the highlight is not the only thing. The area for the mouse to click seems to be small. If it helps, my sprites are centered. Everything else does work though.
 

Simon Gust

Member
Ah yes, centered sprites, the bane of this system.
you either need to put the origins to 0, 0 or do this in the part of the code
Code:
...
// select 
if (point_in_rectangle(mouse_x - wdt/2, mouse_y - hgt/2, xx, yy, xx + wdt, yy + hgt))
{
...
It may also be + wdt/2, I can't remember which one it was.
 

ZyKro

Member
That did the trick. Also, how would I be able to make the buttons go to the next row after a certain amount of buttons have been created. e.g. If I have 1 row of buttons with 6 each in them and want another row after that row is created. Would I have to use two different objects? Or could I implement it in the same object?
 

Simon Gust

Member
In the same object, but with 2D arrays.
Code:
// create
room_id[0, 0] = room_first_level;
room_id[0, 1] = room_second_level;
...
room_id[1, 5] = room_last_level;
lines = 2;
rows = 6;

// draw
var wdt = sprite_get_width(spr_room_buttons);
var hgt = sprite_get_height(spr_room_buttons);
for (var i = 0; i < rows; i++) //6 rows
{
    for (var j = 0; j < lines; j++) //2 lines
    {
        var xx = 20 + i * 32;
        var yy = 20 + j * 32;
      
        // draw
        var index = i * lines + j;
        draw_sprite(spr_room_buttons, index, xx, yy);

        // select
        if (point_in_rectangle(mouse_x - wdt/2, mouse_y - hgt/2, xx, yy, xx + wdt, yy + hgt))
        {
            if (mouse_check_button_pressed(mb_left))
            {
                room_goto(room_id[i, j]);
            }
            draw_sprite(spr_button_highlight, 0, xx, yy);
        }       
    }
}
 
Top