简体   繁体   中英

SFML weird movement

For some reason when I run the code below... The character only moves diagonally down. Lets say line I"m drawing is character:

\
 \
  \
   \
    \

That's the only direction it moves if I press up, right, down or left key!

Please help, I want to go right if right, up if up, down if down, and left if left.

My Code is:

#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();
    }
}

In every case of your switch statement, you move the sprite by (x,y), and then either increment or decrement x or y, depending upon the direction. However, this is fruitless, since on the very next line, you reset them both to 3. So in effect, whatever direction key is pressed, you are doing this:

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

That is, 3 units to the right and 3 units down, which seems to fit your description of the movement you are seeing. I'm not sure what kind of movement you're going for, but here's an example that could work:

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;
}

This would move the sprite 3 units each time a direction key was pressed, in that direction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM