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

How can i detect where i clicked on an object?

S

sulanum

Guest
I have this, but the mouse cursor is on top left, so the click position is not correct.
How can i fix that? and is there a way to check where i have clicked? To check if it is a headshot or not.

 

Tthecreator

Your Creator!
You can get the mouse position using the variables:
Code:
mouse_x
mouse_y
You can get your players position using:
Code:
x
y
Now you need to get the position of the head. You could do this with:
Code:
headx1=x
heady1=y
headx2=headx1+sprite_get_width(sprite_index)
heady2=heady1+ the height of the head in pixels
That gives you the area of the head. Do you understand that code?
Now to get if the mouse is in that area you can use: point_in_rectangle()
Which I'm not going to tell you how to use, because that would mean I'd just gave you all the code.
What you need is use that function in an if statement

If you have any trouble with these last steps, reply below here and tag me @Tthecreator
 
S

sulanum

Guest
Isn't working :s
Nothing seems happen, even tried to create a position check, but the whole script isn't working.
I think, because i click not on the object, but next to it.

i just check the shoot position, if it is within the range.
 

GMWolf

aka fel666
Seems working now, but for some reason, can i shoot in the wall to kill that guy.
Perhaps because you do not have any code to detect if you clicked on a wall?
Remember, GM doesn't care if there is a wall or not, a click is a click.
 
M

Mholmes3038

Guest
The issue if your NPC needs to be in a range. Your telling the mouse that the object only exists at point 640, 448. You need to tell the mouse to detect does the object exsits at this point. Your object its self will exists in a range. I assume you want to shoot a circle. So create a class representing the circle and tell the mouse did I click on the object?

If yes do stuff else missed
 
Top