# Point on object clicked

Get help using Construct 2

### » Mon Jan 30, 2012 3:41 am

Hey guys :) Im new here and this is my first post :P

Ok so im moving away from game maker 8.1 and trying construct 2 for a change and so far im loving the simplicity of it :)

So im currently testing functions and events in construct, however i need help with this.

Lets say i have a ball, and the ball's width is 50 pixels across. Now how can i find exactly where i clicked on this ball and save the x (which would be from 1 - 50) to a global varibale? So if lets say i clicked close to the middle it would record the x as being close to 25? and further to the left of the ball would be from 1-25.

Sorry if its confusing but for the game i am thinking about creating i need to implement this.

Chris
B
1
Posts: 5
Reputation: 235

### » Mon Jan 30, 2012 4:08 am

You can calculate it with the difference between the Mouse and Sprite locations.

+On clicked on Sprite
-set x_on_sprite to Mouse.X-Sprite.X
-set y_on_sprite to Mouse.Y-Sprite.Y

This is dependent on the Sprite's hotspot. If the hotspot is centered then x_on_sprite and y_on_sprite will be -25 to 25, but if the hotspot is top_left then they will be from 0 to 50.
B
75
S
21
G
48
Posts: 4,667
Reputation: 37,199

### » Mon Jan 30, 2012 4:15 am

[QUOTE=R0J0hound] You can calculate it with the difference between the Mouse and Sprite locations.

+On clicked on Sprite
-set x_on_sprite to Mouse.X-Sprite.X
-set y_on_sprite to Mouse.Y-Sprite.Y

This is dependent on the Sprite's hotspot. If the hotspot is centered then x_on_sprite and y_on_sprite will be -25 to 25, but if the hotspot is top_left then they will be from 0 to 50.[/QUOTE]

Thanks, i see where your coming from, however the ball will be constantly moving around the screen and not in a static position.
B
1
Posts: 5
Reputation: 235

### » Mon Jan 30, 2012 4:23 am

See for example this kickups game. IT determines where you clicked on the ball and send it shooting in a specific direction depending on where you clicked.

http://www.spikything.com/games/kickups/
B
1
Posts: 5
Reputation: 235

### » Mon Jan 30, 2012 4:55 am

It will work on a moving object.
Mouse.X-Sprite.X gives you the x position relative to the Sprite, same for the y.

With the hotspot of the sprite set to "top left" you will get values between 0-50 for x and y.

If all you want is the direction from the mouse to the center of the Sprite then you can use the angle() expression to get the angle:
angle(Mouse.X, Mouse.Y, Sprite.X, Sprite.Y)

With the physics behavior the angle expression isn't even needed as you can apply impulse toward a position.
http://dl.dropbox.com/u/5426011/examples%208/kick.capx
B
75
S
21
G
48
Posts: 4,667
Reputation: 37,199