c++ - Sprite 运动

标签 c++ sdl sfml

我正在为这个而撕扯我的头发。出于某种奇怪的原因,我找不到/想不出如何在 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/

相关文章:

opengl - 使用 SDL 2.0 的事件

c - 在 CLION 中安装 SDL

c++ - 有没有一种简单的方法可以在对象 vector 上调用构造函数?

c++ - 带相机旋转的 3d 透视投影

c++ - 在 C++ 中使用 #include 包含多个

c++ - 为什么 `std::initializer_list`经常按值传递?

c++ - 每次处理后删除所有使用的指针

c++ - Mix_PlayMusic 导致内存损坏

C++ "Error expected a ' )'"

c++ - 一个在 C++ 中打印 vector 和列表的函数