Time for action – making Benjamin move
The first thing we want to do is to make our elephant movable. In order to achieve that, we use a QTimer parameter called m_timer, which is a private member of MyScene. In the 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 a 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. This is done by the arrow keys in a way we have already discussed when the m_direction variable of the class Player was introduced. Here is the implementation of what was described there:
void MyScene::keyPressEvent(QKeyEvent *event) {
if (event->isAutoRepeat())
return;
switch (event->key()) {
case Qt::Key_Right:
m_player->addDirection(1);
checkTimer();
break;
case Qt::Key_Left:
m_player->addDirection...