Jack Wright
Jack Wright

Reputation: 17

C++ SFML sprite position not updating when in function

So I have a sprite named player.

If I enter

player.move(3,4);

in my loop then the player moves that much each frame.

However if I make a function.

int updatePlayerPos(sf::Sprite player1)
{
player1.move(3, 4);
return 0;
}

and then call that in the main loop using

updatePlayerPos(player);

It does not do anything.

What mistake am I making?

Thanks in advance.

Upvotes: 0

Views: 686

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409166

It's because the sprite is passed by value, meaning the function have a copy of the sprite. You can do anything with the copy, but the original will not change at all. You would want to pass it by reference instead:

int updatePlayerPos(sf::Sprite& player1)
//                            ^
//          Note the ampersand

Upvotes: 4

Related Questions