我正在为这个而撕扯我的头发。出于某种奇怪的原因,我找不到/想不出如何在 SFML 和/或 SDL 中移动 Sprite 。我看过的这两个库的教程对此一无所知;所以我认为它更像是 C++ 的东西而不是库的东西。
所以我想知道;你如何移动 Sprite ?
(当我说移动时,我的意思是让 Sprite 以设定的速度“滑过”窗口)
最佳答案
您需要一个每秒调用固定次数的循环,然后每帧更新 Sprite 的 x,y 值。
对于 SFML 你有 sprite.move
while (App.IsOpened())
{
// Process events
sf::Event Event;
while (App.GetEvent(Event))
{
if (Event.Type == sf::Event::Closed)
App.Close();
}
// Get elapsed time
float ElapsedTime = App.GetFrameTime();
// Move the sprite
if (App.GetInput().IsKeyDown(sf::Key::Left)) Sprite.Move(-100 * ElapsedTime, 0);
if (App.GetInput().IsKeyDown(sf::Key::Right)) Sprite.Move( 100 * ElapsedTime, 0);
if (App.GetInput().IsKeyDown(sf::Key::Up)) Sprite.Move(0, -100 * ElapsedTime);
if (App.GetInput().IsKeyDown(sf::Key::Down)) Sprite.Move(0, 100 * ElapsedTime);
}
关于c++ - Sprite 运动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4600130/