I'm completely new to sfml, so I was following a tutorial on youtube. I did exactly as the guy did but still I'm having issue with my movements.
Basically what's happening is that if I press a movement key the player (which is just a rectangle shape), moves just for a bit and when I release the key the player returns to it's official position. Why does it returns to it's official position. It just moves one time even if I press the key longer it doesn't move more than just one time.
here is my code:
#include <SFML/Graphics.hpp>
#include <iostream>
using namespace std;
int main()
{
sf::RenderWindow window(sf::VideoMode(512, 512), "SFML Tutorial", sf::Style::Close | sf::Style::Titlebar| sf::Style::Resize);
//Game Loop
while(window.isOpen())
{
//create an event
sf::Event evnt;
//create a rectangle shape called player
sf::RectangleShape player(sf::Vector2f(100.0f, 100.0f));
player.setFillColor(sf::Color::Red);
while(window.pollEvent(evnt))
{
switch(evnt.type)
{
case sf::Event::Closed:
window.close();
break;
case sf::Event::Resized:
cout<<"New window width: "<<evnt.size.width<<" New window height: "<<evnt.size.height<<endl;
break;
case sf::Event::TextEntered:
if(evnt.text.unicode < 128){
printf("%c", evnt.text.unicode);
}
}
} //window event
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Left))
{
player.move(-5.0f, 0.0f);
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Right))
{
player.move(5.0f, 0.0f);
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Up))
{
player.move(0.0f, -5.0f);
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Down))
{
player.move(0.0f, 5.0f);
}
//display player
window.clear();
window.draw(player);
window.display();
}
return 0;
}