Point in Rectangle [SOLVED]

I'm trying to make a a button (a disc) that when you click on it it takes you to anoter room. I'm using point in rectangle to detect mouse enter and mouse event and check_mouse_pressed to detect the button click. It doesn' tdo anything though when I click on it. I did some debugging and its showing that the point in rectangle function isn't returning true at all, even in bounds.

Code:
if (point_in_rectangle(mouse_x, mouse_y,2300, 300, 2550, 400))
{
   show_debug_message("CUrsor is in SaveLoad icon");
    if (mouse_check_button_pressed(mb_left))
    {
        show_debug_message("Clicked SaveLoad button");
        global.RoomBeforeSaveLoad = room;   
        room_set_persistent(FirstLevel, true);

        room_goto(SaveExitRoom);
        draw_set_colour(c_black);   
    }   
    
    
}
 

FredFredrickson

Artist, designer, & developer
GMC Elder
Is the object always in the same position, and the view static?

You're not taking the x, y positions of the object or view into account when you use those values in point_in_rectangle().

One easy way to debug stuff like this is to have an object draw the mouse_x and mouse_y positions every step, so you can see if your numbers are wrong.
 
@FredFrederickson - Yeah I have a debugging text that prints out the mouse position constantly. The cursor is within the disc. Always in the same position, always static.

You're not taking the x, y positions of the object or view into account when you use those values in point_in_rectangle().
What do you mean?
 

samspade

Member
@FredFrederickson - Yeah I have a debugging text that prints out the mouse position constantly. The cursor is within the disc. Always in the same position, always static.



What do you mean?
He means that unless your view and room size are both the same and not moving, it's unlikely that this will be true when you think it is. An easy way to check is his second piece of advice which is to add a draw rectangle call in some object that uses those values.

It does sound like you might have tested that already. If so, then the question might be where is that code?
 

CloseRange

Member
Code:
var x1 = 25;
var y1 = 25;
var x2 = 300;
var y2 = 100;
if(point_in_rectangle(mouse_x, mouse_y, x1, y1, x2, y2))
    show_debug_message("Hello");
draw_rectangle(x1, y1, x2, y2, false);
I did this as a test and it draws a rectangle and when I hover my mouse over it, it shows the message "hello" a lot.
This will work for you too so the problem is something else

What everyone is trying to say is that your button isn't aligned with the point in rectangle.
also MAKE SURE that this code is in the draw event. (or the step event for the point_in_rectangle part)
 

samspade

Member
So I created a rectangle with the same coordinates and the rectangle matched up. So now what?
If you've confirmed that the rectangle matches what you think and the mouse is in the rectangle but not firing, then you should check for reasons for that they could be things like:
  • The object with that code isn't in the room (wasn't placed is deleted etc)
  • The code depends on some other precondition which isn't true
  • That code is in the wrong event
Probably more. Easiest solution is to add a break point on the if line and see if the break point happens. Then in the debugger you can check the variables. If the break point doesn't trigger then you know that something else is wrong.
 
Top