Thanks for coming back to me. I am following a tutorial. I have created a render window, defined a rectangle shape which I called "player" and shaded red. Set up a while loop whilst the render window is open and poll for a windows close or resize. I want to check if a key is pressed and change player position accordingly.
The tutorial is SFML 2.4 For Beginners - 5: Keyboard Input.
by Hilze Vonck on YouTube.
He shows sf::keyboard:key in his tutorial but that doesn't work for me. I have tried to enter left and right as show below but the object does not move with any keys.
Code is as follows:
#include <SFML/Graphics.hpp>
#include <iostream>
int main()
{
sf::RenderWindow window(sf::VideoMode(512, 512), "SFML Tutorial", sf::Style::Close | sf::Style::Resize);
sf::RectangleShape player(sf::Vector2f(100.0f, 100.0f));
player.setFillColor(sf::Color::Red);
while (window.isOpen())
{
sf::Event evnt;
while (window.pollEvent(evnt))
{
switch (evnt.type)
{
case sf::Event::Closed:
window.close();
break;
case sf::Event::Resized:
printf ("Width: %i, Height: %i\n", evnt.size.width, evnt.size.height);
break;
case sf::Event::TextEntered:
if (evnt.text.unicode < 128)
{
printf("%c", evnt.text.unicode);
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
player.move(-0.1f, 0.0f);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
player.move(0.1f, 0.0f);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
player.move(0.0f, -0.1f);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
player.move(0.0f, 0.1f);
}
window.clear();
window.draw(player);
window.display();
}
}
return 0;
}
Many thanks for your time.