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

Author Topic: [SFML 2.0 RC] Mouse and moving sprite  (Read 11043 times)

0 Members and 1 Guest are viewing this topic.

Dosia

  • Newbie
  • *
  • Posts: 2
    • View Profile
[SFML 2.0 RC] Mouse and moving sprite
« on: May 21, 2012, 08:46:32 pm »
Hey, I have a strange problem. When I am moving a cursor on the sfml window and try move a sprite it is moving faster than when i am not moving a cursor. When cursor isnt in window area, sprite is moving slower too. I thought that is something wrong with my code, but I have made a test in clear project and I have still this problem.

Somebody know what is it or could check it on his computer?

#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
 int main()
 {
     sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
         sf::Texture tex;
         tex.create(10,30);
         sf::Sprite sprite;
         sprite.setColor(sf::Color::Green);
         sprite.setTexture(tex);
         sprite.setPosition(200,100);
     while (window.isOpen())
     {
         sf::Event Event;
         while (window.pollEvent(Event))
         {
             if (Event.type == sf::Event::Closed)
                 window.close();
                         if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                                 sprite.move(-10,0);
                         if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                                 sprite.move(10,0);
                         if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                                 sprite.move(0,-10);
                         if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                                 sprite.move(0,10);
                 }
                 window.clear(sf::Color::White);
         window.draw(sprite);
         window.display();
     }
     return EXIT_SUCCESS;
 }
 

Sprite isnt drawing correctly, I saw two small rectangles, but i should see one big. In main project I dont have this problem (probably it is fault my integrated graphics card).

Thank you for sharing with me your time.
« Last Edit: May 21, 2012, 10:18:58 pm by Laurent »

Perde

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
Re: [SFML 2.0 RC] Mouse and moving sprite
« Reply #1 on: May 21, 2012, 09:19:53 pm »
I haven't directly worked with SFML in quite a while, so I'm merely guessing.

sf::Keyboard works outside of whether an event occured or not, but you're processing it inside the event loop.

So basically this is what happens:
You Press left key and the program enters the event-loop. The sprite is moved.
When you move the mouse additionally that counts as different event, so the the loop is being run through another time. Since isKeyPressed will return true this time as well (since it's not linked to events) the sprite will be moved more often.

Long story short. Use sf::Event::KeyPressed or what it's called (check the doc).

Hope this helps.

[edit]
Also there's cpp tags in the forum, you should use them instead.
« Last Edit: May 21, 2012, 09:24:50 pm by Perde »

Dosia

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: [SFML 2.0 RC] Mouse and moving sprite
« Reply #2 on: May 21, 2012, 09:31:27 pm »
I am really grateful. I used sf::Event to this, but when I pressed left and up together sprite is moving only in one direction. I put this functions behind loop and it is ok, sprite is moving in more than one direction and I do not have change in speed. Stupid mistake, but it is the worst kind of mistakes.

Thanks for help.

Perde

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
Re: [SFML 2.0 RC] Mouse and moving sprite
« Reply #3 on: May 21, 2012, 09:35:05 pm »
Just realized using Event::KeyPressed would probably result in a one-time movement anyway. Just another guess though. I should really start working with it again before continuing replying to threads here.  ;D

Celtic Minstrel

  • Jr. Member
  • **
  • Posts: 80
    • View Profile
Re: [SFML 2.0 RC] Mouse and moving sprite
« Reply #4 on: June 26, 2012, 02:07:08 am »
No, key repeat is enabled by default, so you'd continue to get KeyPressed events until you released the key.

EDIT: Argh, I got confused and bumped up an old post. Sorry!
« Last Edit: June 26, 2012, 02:11:38 am by Celtic Minstrel »