Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Wierd Mouvment C++  (Read 3060 times)

0 Members and 2 Guests are viewing this topic.

amanuel2

  • Newbie
  • *
  • Posts: 4
    • View Profile
Wierd Mouvment C++
« on: May 21, 2016, 05:47:59 am »
For some reason when i run the code below... The character only moves diagonaly down.. Lets say line im drawing is character:

    \
     \
      \
       \
        \

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


Erdrick

  • Jr. Member
  • **
  • Posts: 61
    • View Profile
    • Email
Wierd Mouvment C++
« Reply #1 on: May 21, 2016, 06:57:11 am »
Hi.  Why are you setting x and y to 3 after each event? That means you will keep moving right and down.

UchihaKite

  • Newbie
  • *
  • Posts: 33
    • View Profile
    • Email
Re: Wierd Mouvment C++
« Reply #2 on: May 29, 2016, 11:09:33 pm »
I'm fairly certain that Erdrick's observation is the solution to your problem, if you're still having issues with it, just let us know!

Also, instead of the two "using" you have up top of your code, you can just use "using namespace std;"

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Wierd Mouvment C++
« Reply #3 on: May 30, 2016, 07:18:57 am »
Also, instead of the two "using" you have up top of your code, you can just use "using namespace std;"
You can also just omit using entirely and write std:: the few times you use it. This is even more important once you start to use header files. Namespaces exist for a reason...
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

UchihaKite

  • Newbie
  • *
  • Posts: 33
    • View Profile
    • Email
Re: Wierd Mouvment C++
« Reply #4 on: May 30, 2016, 04:02:56 pm »
Also, instead of the two "using" you have up top of your code, you can just use "using namespace std;"
You can also just omit using entirely and write std:: the few times you use it. This is even more important once you start to use header files. Namespaces exist for a reason...

Oooooh that sounds interesting :O I'm still new to C++, I have never used a header before. Could you explain that to me? Sounds like it could benefit the OP as well as myself :D
I don't know everything yet, I'm just trying to toss what little I know to help whoever I can.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Wierd Mouvment C++
« Reply #5 on: May 30, 2016, 08:17:42 pm »
These are so basic concepts, it makes no sense to explain them in a few sentences here. You should rather grab a good C++ book and start reading: ;)

http://stackoverflow.com/q/388242
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything