# SFML - Moving a sprite on mouseclick

I want to be able to move a sprite from a current location to another based upon where the user clicks in the window. This is the code that I have:

#include <SFML/Graphics.hpp>

int main()
{
// Create the main window
sf::RenderWindow App(sf::VideoMode(800, 600), "SFML window");

// Load a sprite to display
sf::Texture Image;
return EXIT_FAILURE;
sf::Sprite Sprite(Image);

// Define the spead of the sprite
float spriteSpeed = 200.f;

// Start the game loop
while (App.IsOpened())
{
if (sf::Keyboard::IsKeyPressed(sf::Keyboard::Escape))
App.Close();

if (sf::Mouse::IsButtonPressed(sf::Mouse::Right)) {
Sprite.SetPosition(sf::Mouse::GetPosition(App).x, sf::Mouse::GetPosition(App).y);
}

// Clear screen
App.Clear();

// Draw the sprite
App.Draw(Sprite);

// Update the window
App.Display();
}

return EXIT_SUCCESS;
}


But instead of just setting the position I want to use Sprite.Move() and gradually move the sprite from one position to another. The question is how? Later I plan on adding a node system into each map so I can use Dijkstra's algorithm, but I'll still need this for moving between nodes.

Direction can be obtained by simple vector math: (TargetPosition - CurrentPosition).normalize()
And then you will update sprites position each frame. So just add to while loop: CurrentPosition += Velocity * ElapsedTimeSinceLastFrame. ElapsedTimeSinceLastFrame is just for sure - if you don't use time and run this application on faster machine (while loop will be executed faster), your sprite will move of course faster.
And finally - you should control, if sprite is at the end of its journey. So control, if (Velocity * ElapsedTime).length() is not more than (TargetPos - CurrentPos).length(). If it is, move sprite to target position and set velocity to zero (or set some flag not to update sprite position till next mouse click).