SFML 奇怪的动作

SFML weird movement

出于某种原因,当我 运行 下面的代码时...角色只会沿对角线向下移动。可以说我画的线是字符:

\
 \
  \
   \
    \

这是我按向上、向右、向下或向左键时它唯一移动的方向!

求助,我想右走右走,上走上走,下走下走,左走走

我的密码是:

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

using std::cout;
using std::endl;

enum Direction
{
    DOWN,
    LEFT,
    RIGHT,
    UP
};

int main()
{
    sf::RenderWindow _Win(sf::VideoMode(600, 600), "Hello World");
    sf::Texture _texture;
    if (!(_texture.loadFromFile("Resources/SPRITE.png")))
    {
        cout << "Could not load iamge" << endl;
    }

    //Source, tell us our starting position.
    //Vector2i = Vector of 2 in SFML
    sf::Vector2i source(1, DOWN/*or 0*/);

    sf::Sprite _sprite(_texture);

    float x = _sprite.getPosition().x;
    float y = _sprite.getPosition().y;
    while (true)
    {
        sf::Event _event;
        while (_Win.pollEvent(_event))
        {
            switch (_event.type)
            {
            case sf::Event::Closed:
                _Win.close();
                exit(1);
                break;
            case sf::Event::KeyPressed:
                switch (_event.key.code)
                {
                case sf::Keyboard::Up:
                    source.y = UP;
                    _sprite.move(sf::Vector2f(x,y--));
                    y = 3, x=3;

                    break;
                case sf::Keyboard::Down:
                    source.y = DOWN;
                    _sprite.move(sf::Vector2f(x, y++));
                    y = 3, x = 3;
                    break;
                case sf::Keyboard::Right:
                    source.y = RIGHT;
                    _sprite.move(sf::Vector2f(x++, y));
                    y = 3, x = 3;
                    break;
                    case sf::Keyboard::Left:
                    source.y = LEFT;
                    _sprite.move(sf::Vector2f(x--, y));
                    y = 3, x = 3;
                    break;
                }
                break;
            }
        }

        //Cropping Out Image
        //Please Look at sprite in resources/Sprite.png
        //When we run this :
        //_sprite.setTextureRect(sf::IntRect( source.x*32 , source.y*32 , 32 , 32 ));
        //Its going to give us the top left corner sprite image. Thats so because
        //we are cropping source.x*32 , which of 32 is the width of the sprite.. So it
        //starts from 1 * 32. 32 is the width of one sprite so it goes to the end of it.
        //Same Applies to the y. source.y * 32. It just goes to the end of the down sprite.
        //As you go down the y increases, 1 * 32 = 32. And 32 is the width of one sprite
        //so it shows body of one full sprite.
        _sprite.setTextureRect(sf::IntRect( source.x*32 , source.y*32 , 32 , 32 ));
        //Clears Window(Flickering..)
        _Win.clear();
        //Draw Sprite
        _Win.draw(_sprite);
        //And Finally Display the Window.
        _Win.display();
    }
}

在 switch 语句的每种情况下,您都将精灵移动 (x,y),然后根据方向增加或减少 x 或 y。然而,这是徒劳的,因为在下一行,你将它们都重置为 3。所以实际上,无论按下方向键,你都在这样做:

_sprite.move(sf::Vector2f(3, 3));

也就是说,向右 3 个单位,向下 3 个单位,这似乎符合您对所看到的运动的描述。我不确定你想要什么样的运动,但这里有一个可行的例子:

switch (_event.key.code) {
case sf::Keyboard::Up:
  _sprite.move(sf::Vector2f(0, -3));
  break;
case sf::Keyboard::Down:
  _sprite.move(sf::Vector2f(0, 3));
  break;
case sf::Keyboard::Right:
  _sprite.move(sf::Vector2f(3, 0));
  break;
case sf::Keyboard::Left:
  _sprite.move(sf::Vector2f(-3, 0));
  break;
}

每次按下方向键时精灵都会在那个方向移动 3 个单位。