Public Group

# Use mouse to move character

This topic is 3524 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've been searching for tutorials for quite long and haven't found anything. I would like to know how can i make my character move to the position on the map where the mouse has clicked. I already have a procedure to define where my mouse is and where my character is in X and Y coordinates so i would like to receive an effective way for moving the character along the map with the mouse. (if someone could explain how can i do the same while the mouse button is still being pushed and go to the new coordinates i would be very happy) :)

##### Share on other sites
You will have to store the coordinates where you clicked with the mouse, and use those as a destination for your character. You can then move him a little closer to those coordinates every frame. If you are familiar with vectors and vector math, it should be pretty easy.

##### Share on other sites
I'm quite a beginner at programming with any of the languages so i think i won't be able to handle the moving with vector maths. I was thinking of a rather easier solution. Get the coordinates of mouse and character, get the removal of each the x and y coordinates and then divide them by a certain amount. After you get the the divided numbers, you use them in a loop so that the character moves every frame closer to the place where the mouse has been clicked. I tried this method but my only problem is that when i begin using the loop, the game freezes (I'm using delphi ). If anyone could explain me how to make the loop so that the game doesn't freeze and the character moves softly i would be very grateful.

##### Share on other sites
Do you mean it freezes during the loop, then continues when the character is already at the target position, so you don't see the animation?
You shouldn't actually have a loop moving the character, but only move it the very first step, and then continue your normal game-loop to draw the game. The next frame you move it one more step.
So you need some kind of variable that keeps track of the current target for the character. Something like:
if targetX != x || target Y != y move character one step towards targetX, targetYredraw game

that you do each frame. So when you want to start moving you just change targetX, targetY to the new position, and then your character will move a little bit each frame after that until it reaches the correct position.

##### Share on other sites
Thanks for all your help. It looks like the answer was in the last post. Because what i did wrong was to define the time for the loop to stop (it will never meet the requirements to stop). Still i get a smooth moving but the character doesn't move in a straight line so i would like to ask for someone who has used vectors in delphi and could maybe help me solve my problem. If needed i can provide with source code and images for program (it is very poor for now).

P.S. Sorry for the delay for my reply but i couldn't connect to the entire site in the past 2 days.

1. 1
2. 2
JoeJ
20
3. 3
frob
19
4. 4
5. 5

• 10
• 10
• 12
• 13
• 9
• ### Forum Statistics

• Total Topics
632199
• Total Posts
3004733

×