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 > 0
to: parent.width
}
NumberAnimation on x {
id: xAnimLeft
running: flags.horizontal < 0
to: 0
}What just happened?
Instead of performing actions immediately, upon pressing a key, we are...