Time for action – another approach to character navigation

Replace the previous key handlers with the following code:

QtObject { id: flags readonly property int speed: 20 property int horizontal: 0 } Keys.onRightPressed: { recalculateDurations(); flags.horizontal = 1 } Keys.onLeftPressed: { if(flags.horizontal != 0) return recalculateDurations() flags.horizontal = -1 } Keys.onUpPressed: jump() Keys.onReleased: { if(event.key == Qt.Key_Right) flags.horizontal = 0 if(event.key == Qt.Key_Left && flags.horizontal < 0) flags.horizontal = 0 } function recalculateDurations() { xAnimRight.duration = (xAnimRight.to-x)*1000/flags.speed xAnimLeft.duration = (x-xAnimLeft.to)*1000/flags.speed } NumberAnimation on x { id: xAnimRight running: flags.horizontal ...

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.