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

Author Topic: Stuttery Movement - Need help making it smooth.  (Read 1411 times)

0 Members and 2 Guests are viewing this topic.

Euqcerin

  • Newbie
  • *
  • Posts: 6
    • View Profile
Stuttery Movement - Need help making it smooth.
« on: November 04, 2012, 04:36:16 pm »
Hi,
I'm currently working on SFML 2.0 project where you control a rectangle that is moving around on a playing field.
The problem I'm having right now is that the movement is very stuttery and inconsistent.

I'll try to copy in some code so you can see what I've done.

This is from my game class, I've taken away the non essential parts.
void Game::GameLoop()
{      
        sf::Event currentEvent;
        while(_mainWindow.pollEvent(currentEvent))
        {
                switch(_gameState)
                {

                case Game::Playing:
                        {
                                player.Update(_mainWindow, currentEvent);
                                break;
                        }
                }
        }
}

This is in my player class:

void Player::Update(sf::RenderWindow& mW, sf::Event Event)
{
        _Event = Event;
        Move(mW);
}
void Player::Move(sf::RenderWindow& mW)
{
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                _sprite.move(-5, 0);
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                _sprite.move(5,0);
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                _sprite.move(0, -5);
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                _sprite.move(0, 5);
}

So I've figured out what some of the problems are, when using isKeyPressed there is a natural short"pause" right after I press the key and then it goes smooth. Is there anyway I can get rid of this ?
And also while holding down 2 keys, up and left for example, it seems like it going alot faster, like it's calling the Update twice and therefor calling the Move twice and therefor updating the position twice as fast.

This is my first time using SFML and I havn't been using C++ for that long either, I started coding in C# and XNA.

Anyways, any help and/or input is appreciated!

BR,
Fredrik

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Stuttery Movement - Need help making it smooth.
« Reply #1 on: November 04, 2012, 04:54:17 pm »
Quote
So I've figured out what some of the problems are, when using isKeyPressed there is a natural short"pause" right after I press the key and then it goes smooth. Is there anyway I can get rid of this ?
Don't call your Update function inside the event loop.
Laurent Gomila - SFML developer

 

anything