SFML - Moving a sprite on mouseclick

Posted by Mike on Game Development See other posts from Game Development or by Mike
Published on 2011-11-24T23:11:19Z Indexed on 2011/11/25 2:20 UTC
Read the original article Hit count: 332

Filed under:
|
|
|
|

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;
    if (!Image.LoadFromFile("cb.bmp"))
        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.

© Game Development or respective owner

Related posts about c++

Related posts about sprites