• 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!

GameMaker point_in_rectangle() problem, it doesn't work

Hey,

I'm trying to get buttons with the code below but it doesn't work, I really can't see what is the problem.

Draw Event
Code:
draw_rectangle(x-sprite_width/2,y,x+sprite_width/2,y-sprite_height,true);
if (drawTheShop == true)
 {
  //code here...
 }
draw_circle(mouse_x,mouse_y,6,true);
Step Event
Code:
if (point_in_rectangle(mouse_x,mouse_y,x-sprite_width/2,y,x+sprite_width/2,y-sprite_height))
{
 if (mouse_check_button_pressed(mb_left))
 {
  drawTheShop = true;
  image_index++;
  if (FoundryLevel < 3)
   {
     FoundryLevel++;   
   } else {FoundryLevel = 1;}
 }
}
1png.png
I've checked if theres something special about the function in manual but no..
When using this function, you define a rectangular area and GameMaker Studio 2 will work out whether the given point falls within its bounds or not. If the point falls within the defined rectangle the function will return true otherwise the function will return false.
 

ophelius

Member
if (point_in_rectangle(mouse_x,mouse_y, x-sprite_width/2, y-sprite_height/2, x+sprite_width/2, y+sprite_height/2))
 

vdweller

Member
A naive (but also fast) approach for checking if a point is within a rectangle is usually (internally) implemented as
GML:
return (px>=x1 && px<=x2 && py>=y1 && py<=y2)
You can easily see by replacing your initial code that this won't evaluate to true. So in general your x2 and y2 coordinates should be bigger than the x1 and y1 respectively. So think of x1,y1 as the "top-left" corner of your rectangle and x2,y2 as the "bottom-right" one.

EDIT: Manual actually mentions this in detail:
pxThe x coordinate of the point to check.
pyThe y coordinate of the point to check.
x1The x coordinate of the left side of the rectangle to check.
y1The y coordinate of the top side of the rectangle to check.
x2The x coordinate of the right side of the rectangle to check.
y2The y coordinate of the bottom side of the rectangle to check.
 
Last edited:
Top