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 ...
Get Game Programming Using Qt 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.