You can do this by adding a velocity property which dictates the speed and direction of your sprite.
struct MyThing final
{
sf::Sprite sprite;
sf::Vector2f velocity;
};
Set the velocity y value (the direction you want to move) to a certain amount, eg: 10 which will be the number of units it moves per second. Move the sprite by this velocity multiplied by the elapsed time:
while(window.isOpen())
{
//handle events here
sf::Time elapsed = clock.restart();
myThing.sprite.move(myThing.velocity * elapsed.asSeconds());
window.clear();
window.draw(myThing.sprite);
window.display();
}
This will move the sprite in a straight line as defined by the velocity. To make it change direction add a position check after the sprite has moved:
auto position = myThing.sprite.getPosition();
if (position.y < 0)
{
//clamp the position to the bounds
position.y = 0;
myThing.sprite.setPosition(position);
//reverse the direction
myThing.velocity.y = -myThing.velocity.y;
}
else if (position.y > 352)
{
//see above
}
This post offers a more detailed explanation:
https://medium.com/@swiftsnippets/vectors-position-velocity-and-direction-b85342ed9e3a