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.
#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;
}
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.
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.