按下键盘时 SFML 精灵不移动

SFML sprite not moving when pressing keyboard

我正在创建一个简单的游戏来使用键盘按钮 (ASDW) 移动精灵,并且在我的代码中使用 OOP 原则。 一切似乎都很好,没有语法错误,但是当我 运行 代码时,精灵根本没有移动。

这是我控制键盘事件的一段代码:

void Game::proccessEvent()
{
sf::Event event;
while (mWindow.pollEvent(event)) {
    switch (event.type) {
        case sf::Event::Closed:
            mWindow.close();
            break;
            
        case sf::Event::KeyPressed:
            handlePlayerInput(event.key.code, true);
            break;
            
        case sf::Event::KeyReleased:
            handlePlayerInput(event.key.code, true);
            break;
            
        default:
            break;
    }
}
}

我也把我的整个代码粘贴在这个 link 中: https://pastebin.com/N9EH9yLi

谁能帮我解决这个问题?我很困惑。

我查看了您的代码并发现了一些问题。

  1. 首先,您没有初始化所有 Game 对象成员变量,特别是确定向上、向下、向左或向右移动的布尔标志。
  2. 其次,播放器速度的值非常低 const float playerSpeed = 0.1f; 它变得更不引人注意,因为您的代码乘以 1/60 秒的增量时间,这导致几乎不引人注意的值。我将值更改为 100.f 只是为了验证它是否有效。
  3. 第三,当释放键时,代码无法禁用布尔标志。这是通过将 handlePlayerInput(event.key.code, true); 更改为 handlePlayerInput(event.key.code, false); 来解决的,即,在释放该键时禁用沿该方向的移动。
  4. 第四,不确定这是不是故意的,但是你禁止用户同时在 x 和 y 方向上移动。如果不是,则使用两个单独的 if/else 块检查垂直和水平方向的移动。

https://pastebin.com/Mx4vzmmL

#include <SFML/Graphics.hpp>
#include <iostream>
class Game
{
public:
    Game();
    void run();

private:
    void proccessEvent();
    void update(sf::Time deltaTime);
    void render();
    void handlePlayerInput(sf::Keyboard::Key key, bool isPressed);

private:
    const float playerSpeed = 100.f;
    const sf::Time TimePerFrame = sf::seconds(1.0f / 60.0f);

    sf::RenderWindow mWindow;
    sf::Keyboard::Key key;
    sf::Texture texture;
    sf::RectangleShape rect;
    bool isPressed;
    bool mIsMovingUp;
    bool mIsMovingDown;
    bool mIsMovingRight;
    bool mIsMovingLeft;
};
Game::Game()
    : //Make sure to initialize member variables.
    playerSpeed(100.f),
    TimePerFrame(sf::seconds(1.f / 60.f)),
    mWindow(sf::VideoMode(640, 480), "SFML Application"),
    key(sf::Keyboard::Unknown),
    isPressed(false),
    mIsMovingUp(false),
    mIsMovingDown(false),
    mIsMovingRight(false),
    mIsMovingLeft(false)
{
    //  rect.setTexture(texture);
    rect.setFillColor(sf::Color::Red);
    rect.setSize(sf::Vector2f(20.0f, 20.0f));
}

void Game::run()
{
    sf::Clock clock;
    sf::Time timeSinceLastUpdate = sf::Time::Zero;

    while (mWindow.isOpen())
    {
        proccessEvent();
        sf::Time elapsedTime = clock.restart();
        timeSinceLastUpdate += elapsedTime;
        while (timeSinceLastUpdate > TimePerFrame)
        {
            timeSinceLastUpdate -= TimePerFrame;
            proccessEvent();
            update(TimePerFrame);
        }
        render();
    }
}

void Game::proccessEvent()
{
    sf::Event event;
    while (mWindow.pollEvent(event)) {
        switch (event.type) {
        case sf::Event::Closed:
            mWindow.close();
            break;
        case sf::Event::KeyPressed:
            handlePlayerInput(event.key.code, true);
            break;
        case sf::Event::KeyReleased:
            handlePlayerInput(event.key.code, false);
            break;
        default:
            break;
        }
    }
}

void Game::update(sf::Time deltaTime)
{

    sf::Vector2<float> movement(0.0f, 0.0f);
    if (mIsMovingUp)
    {
        movement.y -= playerSpeed;
    }
    else if (mIsMovingDown)
    {
        movement.y += playerSpeed;
    }
    if (mIsMovingRight)
    {
        // You were subtracting to move right, add instead.
        movement.x += playerSpeed;
    }
    else if (mIsMovingLeft)
    {
        movement.x -= playerSpeed;
    }
    rect.move(movement * deltaTime.asSeconds());

}

void Game::render()
{
    //if (!texture.loadFromFile("/Users/tahajalili/Desktop/Eagle.png"))
    //{
    //  std::cout << "Error occured" << std::endl;
    //}

    mWindow.clear();
    mWindow.draw(rect);
    mWindow.display();
}

void Game::handlePlayerInput(sf::Keyboard::Key key, bool isPressed)
{
    if (key == sf::Keyboard::W)
    {
        mIsMovingUp = isPressed;
    }
    else if (key == sf::Keyboard::S)
    {
        mIsMovingDown = isPressed;
    }
    else if (key == sf::Keyboard::D)
    {
        mIsMovingRight = isPressed;
    }
    else if (key == sf::Keyboard::A)
    {
        mIsMovingLeft = isPressed;
    }
}

int main(int, char const**)
{
    Game game;
    game.run();
}

我不确定您是否有意使用关键事件来移动精灵,但更好更简单的替代方法是调用 sf::Keyboard::isKeyPressed 每次更新而不是跟踪依赖于事件的布尔变量.