C++ w/ SFML Smooth Movement Help

I'm really stuck here. I'm trying to make smooth movement with my sprite, but I get this annoying delay whenever I hold down one of the movement keys. I've been researching this for hours and I'm still at a dead end.

Here's the code. I bet I'm missing something really simple.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "SFML/Graphics.hpp"
#include "SFML/Audio.hpp"
#include "SFML/Window.hpp"
#include <iostream>

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "S H A D E");
    window.setFramerateLimit(60);

    //LOAD GAME MUSIC
    sf::Music gameMusic;
    if (!gameMusic.openFromFile("music/Quirky_Dog.ogg")) {
        std::cerr << "No music file found!" << std::endl;
    }
    //LOAD GAME TEXTURE
    sf::Texture sprite;
    if (!sprite.loadFromFile("images/test_sprite.png")) {
        std::cerr << "No sprite image found!" << std::endl;
    }

    //LOAD GAME SPRITE
    sf::Sprite player(sprite);
    player.setPosition(400, 300);

    //LOAD GAME MUSIC
    gameMusic.play();

    //PLAYER MOVEMENT VARIABLES
    float playerMovementSpeed = 10;

    while (window.isOpen())
        {
            sf::Event event;
            while (window.pollEvent(event))
            {
                if (event.type == sf::Event::Closed)
                    window.close();
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
                    window.close();

                //PLAYER MOVEMENT
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                    player.move(-playerMovementSpeed, 0);
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                    player.move(0, playerMovementSpeed);
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                    player.move(0, -playerMovementSpeed);
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                    player.move(playerMovementSpeed, 0);
            }

            window.clear();
            window.draw(player);
            window.display();
        }

    return 0;
}


Thanks all.
Your keyboard checks are inside the event loop, which means that code will only run when there is an even to process (and will run multiple times if there are multiple events).

Move lines 43-50 outside of that loop. Those have nothing to do with events.
@Disch
Your keyboard checks are inside the event loop, which means that code will only run when there is an even to process (and will run multiple times if there are multiple events).

Move lines 43-50 outside of that loop. Those have nothing to do with events.


Woah, that fixed it. Thanks a ton!
Topic archived. No new replies allowed.