C++ C++;SFML精灵位置在运行时未更新

C++ C++;SFML精灵位置在运行时未更新,c++,sfml,C++,Sfml,所以我有一个叫player的精灵 如果我进去 player.move(3,4); 在我的循环中,玩家每一帧都会移动那么多 但是如果我做一个函数 int updatePlayerPos(sf::Sprite player1) { player1.move(3, 4); return 0; } 然后在主循环中使用 updatePlayerPos(player); 它没有任何作用 我犯了什么错误 提前感谢。这是因为精灵是按值传递的,这意味着函数有精灵的副本。你可以对副本做任何事情,但原件一点也不

所以我有一个叫player的精灵

如果我进去

player.move(3,4);
在我的循环中,玩家每一帧都会移动那么多

但是如果我做一个函数

int updatePlayerPos(sf::Sprite player1)
{
player1.move(3, 4);
return 0;
}
然后在主循环中使用

updatePlayerPos(player);
它没有任何作用

我犯了什么错误


提前感谢。

这是因为精灵是按值传递的,这意味着函数有精灵的副本。你可以对副本做任何事情,但原件一点也不会改变。您可能希望通过引用来传递它:

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

这是因为精灵是按值传递的,这意味着函数有精灵的副本。你可以对副本做任何事情,但原件一点也不会改变。您可能希望通过引用来传递它:

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