What just happened?

The player itself is an empty item with a keyboard focus that handles presses of the right, left, and up arrow keys, causing them to manipulate the x and y coordinates of the player. The x property has a Behavior element set so that the player moves smoothly within the scene. Finally, anchored to the player item is the actual visualization of the player—our elephant friend.

When the right or left arrow keys are pressed, a new position for the character will be calculated and applied. Thanks to the Behavior element, the item will travel gradually (during one second) to the new position. Keeping the key pressed will trigger autorepeat and the handler will be called again. In a similar fashion, when the up arrow key is pressed, ...

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.