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

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.