2016-05-21 7 views
0

私は以下のコードを実行すると何らかの理由で...文字が斜めに動くだけです。私は "メートルの描画が文字列としましょう!SFML変わった動き

\ 
\ 
    \ 
    \ 
    \ 

私が押した場合、それがダウンしているか、左キー、右、移動方向のみだ

助けてください、私は、右の場合は右に行きたいです。アップであればアップ、ダウンダウンの場合、左ならば左

マイコードがある:あなたのswitch文のすべての場合には

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

答えて

1

、あなたはどちらか、その後の(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台に方向キーが押されたそれぞれの時間を移動します。

+0

ありがとうございます!... –

関連する問題