Input processing

A usual approach in games is to read input events and call functions responsible for actions associated with particular events:

void Scene::keyEvent(QKeyEvent *event) {
    switch(event->key()) {
    case Qt::Key_Right:         player->goRight(); break;
    case Qt::Key_Left:          player->goLeft();  break;
    case Qt::Key_Space:         player->jump();    break;
    // ...
    }
} 

This, however, has its drawbacks, one of which is the need to check events at even periods of time. This might be hard and is certainly not a declarative approach.

We already know that Qt Quick handles keyboard input via the Keys attached property. It is possible to craft QML code similar to the one just presented, but the problem with such an approach is that the faster the player taps keys ...

Get Game Programming using Qt 5 Beginner's Guide - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.