1. Hey! Guest! The 35th GMC Jam will take place between November 28th, 12:00 UTC - December 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

Point in Rectangle [SOLVED]

Discussion in 'Programming' started by Ted Gress, Oct 24, 2018.

  1. Ted Gress

    Ted Gress Member

    Joined:
    Sep 1, 2016
    Posts:
    752
    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);   
        }   
        
        
    }
    
     
  2. FredFredrickson

    FredFredrickson Member GMC Elder

    Joined:
    Jun 21, 2016
    Posts:
    120
    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.
     
  3. Dr_Nomz

    Dr_Nomz Member

    Joined:
    Oct 31, 2016
    Posts:
    594
  4. Ted Gress

    Ted Gress Member

    Joined:
    Sep 1, 2016
    Posts:
    752
    @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?
     
  5. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    2,082
    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?
     
    FredFredrickson likes this.
  6. Ted Gress

    Ted Gress Member

    Joined:
    Sep 1, 2016
    Posts:
    752
    So I created a rectangle with the same coordinates and the rectangle matched up. So now what?
     
  7. CloseRange

    CloseRange Member

    Joined:
    Jul 2, 2016
    Posts:
    875
    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)
     
  8. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    2,082
    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.
     
    FredFredrickson likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice