How do I make a sprite moving in the mouse direction

Get help using Construct 2

Post » Mon Jan 02, 2017 2:00 am

Hey!

I wanted to know how I can make a sprite ( basically a player here ) moving to the mouse position after clicking, for example like in League Of Legends, where your character will move to the clicked position.

Thanks !
B
3
Posts: 1
Reputation: 177

Post » Mon Jan 02, 2017 5:35 am

you need 3 localvariables or globalvariables, 3 numbers lets name them targetx and targety for position of mouse, and 1 boolean, that will toggle the movement, lets call it move


and then we create the bellow pseudo code.

Code: Select all
on left click - set targetx to mouse.x
              - set targety to mouse.y
              - set move to 1

if move =1   player set angle towards position targetx targety

                  - move forward 5 pixels

/////////////////////////////////        now we add the condition to stop the movement        /////////////////////////////

compare two variables expression from system and we add the distance formula distance(x1,y1,x2,y2)

distance(player.x,player.y,targetx,target.y) =< 20  -  set move to 0

and that is it.





and here is the capx move to point


you may want to adjust things inside for optimisation like trigger once on distance =< 20 and change the global variables to localvariables on the player so you save events
B
78
S
23
G
69
Posts: 1,353
Reputation: 44,005

Post » Mon Jan 02, 2017 10:21 am

Program Files\Construct 2\Examples folder. There's tons of great and ready to use examples, just like "move to mouse.capx".
ImageImageImageImage
B
158
S
66
G
43
Posts: 2,603
Reputation: 35,868


Return to How do I....?

Who is online

Users browsing this forum: dop2000 and 5 guests