Time for action - Making Benjamin move

The next thing we want to do is make our elephant movable. In order to achieve that, we add a QTimer m_timer private member to MyScene. QTimer is a class that can emit the timeout() signal periodically with the given interval. In the MyScene constructor, we set up the timer with the following code:

m_timer.setInterval(30);
connect(&m_timer, &QTimer::timeout, 
this, &MyScene::movePlayer);

First, we define that the timer emits the timeout signal every 30 milliseconds. Then, we connect that signal to the scene's slot called movePlayer(), but we do not start the timer yet. The timer will be started when the player presses a key to move.

Next, we need to handle the input events properly and update the player's direction. We introduce the Player * m_player field that will contain a pointer to the player object and the int m_horizontalInput field that will accumulate the movement commands, as we'll see in the next piece of code. Finally, we reimplement the keyPressEvent virtual function:

void MyScene::keyPressEvent(QKeyEvent *event)
{
    if (event->isAutoRepeat()) {
        return;
    }
    switch (event->key()) {
    case Qt::Key_Right:
        addHorizontalInput(1);
        break;
    case Qt::Key_Left:
        addHorizontalInput(-1);
        break;
    //...
    }
}
void MyScene::addHorizontalInput(int input)
{
    m_horizontalInput += input;
    m_player->setDirection(qBound(-1, m_horizontalInput, 1));
    checkTimer();
}
As a small side note, whenever code snippets in the following code passages are irrelevant for the actual detail, we will skip the code but will indicate missing code with //... so that you know that it is not the entire code. We will cover the skipped parts later when it is more appropriate.