Jump to content
  • Advertisement
Sign in to follow this  
Alex131

Use mouse to move character

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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, targetY

redraw 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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!