Hi, when I try to move a sprite by the key board but I have to move the mouse for it to move.
Any help? The code is below
#include <SFML/Graphics.hpp>
void initShape(sf::RectangleShape& shape, sf::Vector2f const& pos, sf::Color const& color){
shape.setFillColor(color);
shape.setPosition(pos);
shape.setOrigin(shape.getSize()*0.5f);}
int main()
{
sf::RenderWindow window(sf::VideoMode(480, 180), "My window");
window.setFramerateLimit(60);
sf::Vector2f startPos = sf::Vector2f(50, 50);
sf::RectangleShape playerRect(sf::Vector2f(50, 50));
initShape(playerRect, startPos, sf::Color::Green);
sf::RectangleShape targetrect(sf::Vector2f(50,50));
initShape(targetrect, sf::Vector2f(400, 50), sf::Color::Yellow);
sf::RectangleShape Badrect(sf::Vector2f(50, 100));
initShape(Badrect, sf::Vector2f(250, 50), sf::Color::Red);
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed){
window.close();}
playerRect.move(1,0);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)){playerRect.move(0,1);}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){playerRect.move(0,-1);}
if(playerRect.getGlobalBounds().intersects(targetrect.getGlobalBounds())){window.close();}}
if(playerRect.getGlobalBounds().intersects(Badrect.getGlobalBounds())){playerRect.setPosition(startPos);}
window.clear(sf::Color::Black);
window.draw(playerRect);
window.draw(Badrect);
window.draw(targetrect);
window.display();}
return 0;
}