I am a beginner in anything with game development. I written a small prototype in which i can move a square around with the arrow keys. For that i use SFML.
At the moment i experiment around with my Event System: First, to keep things simple, i was polling for the arrow keys using sf::Keyboard::isKeyPressed(), which works fine for this small kind of "game"
// in handleUserInput():
if(Keyboard::isKeyPressed(Keyboard::Left))
{
player_.moveLeft();
}
// in player the player class:
void moveLeft()
{
velocity.x = -x_speed; // sf::Vector2f
}
// in my player-class update() method:
void update()
{
// Out of bounds check and so on...
shape.move(velocity);
velocity.x = 0;
velocity.y = 0;
}
With this i was able to move horizontally, vertically and diagonally.
Then i moved to create my "real" EventSystem based on callbacks. It wraps around sf::Window::pollEvent(). E.g. i register a callback method for the event type sf::Event::EventType::KeyPressed. So i changed my code to first register for the movement keys
event_system_.registerCallback(sf::Event::KeyPressed, [&] (const sf::Event& event)
{
switch(event.key.code)
{
case sf::Keyboard::Left:
log->info() << "Moving Left";
player_.moveLeft();
break;
case sf::Keyboard::Right:
log->info() << "Moving Right";
player_.moveRight();
break;
case sf::Keyboard::Up:
log->info() << "Moving Up";
player_.moveUp();
break;
case sf::Keyboard::Down:
log->info() << "Moving Down";
player_.moveDown();
break;
case sf::Keyboard::Escape:
window_.close();
break;
}
});
The problem i encountered was, that i couldn't move diagonally anymore. After adding the loggin parts i found out, that when i keep two arrow keys pressed the only event that gets fired over and over again was the key pressed latest (e.g. i try to press Left & Down and pressed the Down-Key slightly later the Down-Event would be captured over and over again and the Left-Event only once).
After that i changed my code not to reset the velocity in my player's update-method and also registered for sf::Event::KeyReleased so that it would reset the x/y-part of the velocity when the corresponding key was released.
event_system_.registerCallback(sf::Event::KeyReleased, [&] (const sf::Event& event)
{
switch(event.key.code)
{
case sf::Keyboard::Left:
case sf::Keyboard::Right:
log->info() << "Stopped moving horizontally";
player_.velocity.x = 0;
break;
case sf::Keyboard::Up:
case sf::Keyboard::Down:
log->info() << "Stopped moving vertically";
player_.velocity.y = 0;
break;
}
});
But somehow this does not seem right to me (But it wouldn't seem right to me to poll for every single possible input manually - So i think events is the things to take). Could anyone give me a hint what i am doing wrong and how to do it better?
Kind Regards