O'Reilly logo

Game Programming using Qt 5 Beginner's Guide - Second Edition by Lorenz Haas, Witold Wysota, Pavel Strakhov

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

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