in this code i tried to make a basic circle move via wasd key combo, but as soon as i start it up the circle starts moving to the left without keyboard input?
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
class Control{
public:
sf::RenderWindow window;
sf::CircleShape circle;
bool move_up, move_down, move_left, move_right;
//int speed = 1;
Control()
: window(sf::VideoMode(600, 480), "test")
, circle(){
circle.setRadius(40.f);
circle.setPosition(100.f, 100.f);
circle.setFillColor(sf::Color::Cyan);
}
void run(){
while (window.isOpen()){
process_events();
update();
render();
}
}
void process_events(){
sf::Event event;
while (window.pollEvent(event)){
if (event.type == sf::Event::Closed){
window.close();
}
else if (event.type == sf::Event::KeyPressed){
handler(event.key.code, true);
}
else if (event.type == sf::Event::KeyReleased){
handler(event.key.code, false);
}
}
}
void handler(sf::Keyboard::Key key, bool is_pressed){
if (key == sf::Keyboard::W)
move_up = is_pressed;
else if (key == sf::Keyboard::S)
move_down = is_pressed;
else if (key == sf::Keyboard::A)
move_left = is_pressed;
else if (key == sf::Keyboard::D)
move_right = is_pressed;
}
void update(){
sf::Vector2f movement(0.f, 0.f);
if (move_up)
movement.y -= 1.f;
if (move_down)
movement.y += 1.f;
if (move_left)
movement.x -= 1.f;
if (move_right)
movement.x += 1.f;
circle.move(movement);
}
void render(){
window.clear();
window.draw(circle);
window.display();
}
};
int main(){
Control app;
app.run();
}