O'Reilly logo

Game Programming Using Qt by Lorenz Haas, Witold Wysota

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required