首先,我已经在SFML论坛上提出了这个问题,但他们无济于事,他们实际上复制了我的代码,即使在与我具有相同操作系统的人中(Windows 8),它也能正常工作

我目前正在阅读SFML游戏开发书,在第一章中介绍了运动时,遇到了一些问题,我在窗口中心生成了一个白色的圆圈,现在如果按任意方向箭头,该圆圈会继续前进在另一个方向上直到我按下它,然后它恢复正常,水平和垂直方向分开。

示例:我开始“游戏”,按向上,圆圈继续向下,我按住,圆圈静止,我释放,它继续向下,我按下,停止,现在它正常响应两个键,将会再次左右移动,它会在初始错误后得到修复,但是我想知道如何删除它

#include <SFML/Graphics.hpp>
using namespace sf;

class Game
{
    public:Game();
        void run();

    private:
        void processEvents();
        void update(Time);
        void render();
        void handlePlayerInput(Keyboard::Key,bool);

        bool mIsMovingUp, mIsMovingRight, mIsMovingLeft, mIsMovingDown;
        float playerSpeed;
        Time TimePerFrame;


    private:
        RenderWindow mWindow;
        CircleShape mPlayer;
};

Game::Game():mWindow(VideoMode(640, 480), "SFML Application"),mPlayer(), playerSpeed(20.f), TimePerFrame(seconds(1.f / 60.f))
{
    mPlayer.setRadius(20.f);
    mPlayer.setPosition(220.f, 220.f);
    mPlayer.setFillColor(Color::White);
}

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

void Game::run()
{
    Clock clock;
    Time timeSinceLastUpdate = Time::Zero;
    while (mWindow.isOpen())
    {
        processEvents();
        timeSinceLastUpdate += clock.restart();
        while (timeSinceLastUpdate > TimePerFrame)
        {
            timeSinceLastUpdate -= TimePerFrame;
            processEvents();
            update(TimePerFrame);
        }
    render();
    }
}

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

void Game::update(Time deltaTime)
{
    Vector2f movement(0.f, 0.f);
    if (mIsMovingUp)
        movement.y -= playerSpeed;
    if (mIsMovingDown)
        movement.y += playerSpeed;
    if (mIsMovingLeft)
        movement.x -= playerSpeed;
    if (mIsMovingRight)
        movement.x += playerSpeed;

    mPlayer.move(movement * deltaTime.asSeconds());
}

void Game::render()
{
    mWindow.clear();
    mWindow.draw(mPlayer);
    mWindow.display();
}

int main()
{
    Game game;
    game.run();
}

最佳答案

回答:

默认情况下,这些变量必须为true:

bool mIsMovingUp, mIsMovingRight, mIsMovingLeft, mIsMovingDown;

Game::Game()中,将它们全部声明为false。那应该解决您的问题。

说明:

换句话说,当mIsMovingUp和down为mIsMovingDown true时,它保持静止。当您向上按并放开时,它使mIsMovingUp变为false,但使mIsMovingDown保持为true,因此球将移动mIsMovingDown。按下并放开mIsMovingDown之后,它会变成mIsMovingDown false,然后它们都是false,并且移动停止。 isMovingLeftisMovingRight相同。

关于c++ - SFML自动机芯,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17901309/

10-12 21:31