c++ - 当我移动鼠标时,SFML Player 移动得更快

标签 c++ sfml

我是 SFML 新手,正在学习教程。然而,我遇到了一个非常奇怪的错误。我有一个简单的脚本,每当按 WASD 时都会运行player.move。
尽管此脚本有效,但玩家的移动速度极慢。但是,当我在移动时移动鼠标时,它会以正确的速度移动。
我有 Visual Studio 2022 和 SFML 2.6。

我在互联网上研究了很多问题,但找不到有类似问题的人。
我的代码是

#include <SFML\Graphics.hpp>
#include <iostream>

int main() {
    sf::RenderWindow window(sf::VideoMode(512, 512), "SFML Tutorial", sf::Style::Close | sf::Style::Resize);
    sf::RectangleShape player(sf::Vector2f(100.0f, 100.0f));
    player.setFillColor(sf::Color::Red);
    while (window.isOpen())
    {
        sf::Event evnt;
        while (window.pollEvent(evnt)) {
            switch (evnt.type) {
            case sf::Event::Closed:
                window.close();
                break;
            case sf::Event::Resized:
                printf("New Window Width: %i New Window Height: %i\n", evnt.size.width, evnt.size.height);
                break;
            case sf::Event::TextEntered:
                if (evnt.text.unicode < 128) {
                    printf("%c", evnt.text.unicode);
                }
            }

            if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)) {
                player.move(-0.1f, 0.0f);
            }
            if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D)) {
                player.move(0.1f, 0.0f);
            }
            if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W)) {
                player.move(0.0f, -0.1f);
            }
            if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S)) {
                player.move(0.0f, 0.1f);
            }
            window.clear();
            window.draw(player);
            window.display();
        }
    }
    return 0;
}

我没有看到任何检测到鼠标移动的地方,所以我对这个问题感到非常困惑。

最佳答案

您需要测量自上次在主输入循环中迭代以来已经过去了多少时间,然后使用该时间乘以玩家的移动。另外,不要在 while (window.pollEvent(evnt)) 循环内检查是否按下了按键。

类似这样的事情:

sf::Clock clock;
float time{};

while (window.isOpen()) {
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)) {
        player.move(-10.0f * time, 0.0f);                    // multiply
    }

    // ... do the same on the rest too ...

    time = clock.restart().asSeconds(); // last thing in the loop
}

它可能看起来像这样:

#include <SFML/Graphics.hpp>

int main() {
    sf::RenderWindow window(sf::VideoMode(512, 512), "SFML Tutorial",
                            sf::Style::Close | sf::Style::Resize);
    window.setFramerateLimit(120); // not strictly needed

    sf::RectangleShape player(sf::Vector2f(100.0f, 100.0f));
    player.setFillColor(sf::Color::Red);

    sf::Clock clock;
    float time{};
    float speed = 10.0f;

    while(window.isOpen()) {
        sf::Event evnt;
        while(window.pollEvent(evnt)) {
            switch(evnt.type) {
            case sf::Event::Closed:
                window.close();
                break;
            }
        }

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)) {
            player.move({-speed * time, 0.0f});
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D)) {
            player.move({speed * time, 0.0f});
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W)) {
            player.move({0.0f, -speed * time});
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S)) {
            player.move({0.0f, speed * time});
        }
        window.clear();
        window.draw(player);
        window.display();
        time = clock.restart().asSeconds();
    }
}

关于c++ - 当我移动鼠标时,SFML Player 移动得更快,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76648766/

相关文章:

c++ - 希望定期执行一个函数,但继续执行其余的程序循环

c++ - 类型检查失败

c++ - 为什么复制构造函数调用其他类的默认构造函数?

c++ - 如何在不同流程/系统中的 Rhapsody 模型之间进行通信?

c++ - 通过 C++ 与 erlang 服务器通信(发送二进制文件)

c++ - SFML/C++ Sprite 由于超出范围而显示为白框,不知道在哪里

c++ - GDB - 如何从一开始就进入步进模式

c++ - 如何在 Qt 中使用枚举?

sfml - 使用 SFML 我应该注意哪些问题和问题?

c++ - 找不到 SFML dll