June 2013
Beginner to intermediate
296 pages
6h 50m
English
Up to now, you have seen how events and real-time inputs are handled. In our game, we might handle them as follows (the aircraft methods are fictional to show the principle):
// One-time events
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::KeyPressed
&& event.key.code == sf::Keyboard::X)
mPlayerAircraft->launchMissile();
}
// Real-time input
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
mPlayerAircraft->moveLeft();
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
mPlayerAircraft->moveRight();There are several problems with this approach. On one side, we have hardcoded keys, so a user cannot choose WASD instead of arrow keys for movement. On the other side, we ...
Read now
Unlock full access