You still did it the incorrect way.
Try to spot difference between these two codes.
1 The correct way to achieve smooth
int main()
{
sf::RenderWindow win;
win.create(sf::VideoMode(800,600,32), "str");
win.setVerticalSyncEnabled(true);
sf::RectangleShape player;
player.setSize(sf::Vector2f(100,100));
player.setFillColor(sf::Color::Cyan);
player.setPosition(0, 0);
bool WP = false, SP = false, AP = false, DP = false;
while(win.isOpen())
{
win.clear();
sf::Event event;
while(win.pollEvent(event))
{
switch(event.type)
{
case sf::Event::Closed:
win.close();
break;
case sf::Event::KeyPressed:
switch(event.key.code)
{
case sf::Keyboard::W:
WP = true;
break;
case sf::Keyboard::S:
SP = true;
break;
case sf::Keyboard::A:
AP = true;
break;
case sf::Keyboard::D:
DP = true;
break;
}
break;
case sf::Event::KeyReleased:
switch(event.key.code)
{
case sf::Keyboard::W:
WP = false;
break;
case sf::Keyboard::S:
SP = false;
break;
case sf::Keyboard::A:
AP = false;
break;
case sf::Keyboard::D:
DP = false;
break;
}
break;
}
}
if(WP)
player.move(0, -10);
else if(SP)
player.move(0, 10);
if(AP)
player.move(-10, 0);
else if(DP)
player.move(10, 0);
win.draw(player);
win.display();
}
return 0;
}
2 The incorrect way the one you used
int main()
{
sf::RenderWindow win;
win.create(sf::VideoMode(800,600,32), "str");
win.setVerticalSyncEnabled(true);
sf::RectangleShape player;
player.setSize(sf::Vector2f(100,100));
player.setFillColor(sf::Color::Cyan);
player.setPosition(0, 0);
while(win.isOpen())
{
win.clear();
sf::Event event;
while(win.pollEvent(event))
{
if(event.type == sf::Event::Closed)
win.close();
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
player.move(0, -10);
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
player.move(0, 10);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
player.move(-10, 0);
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
player.move(10, 0);
win.draw(player);
win.display();
}
return 0;
}