Time for action – Another approach to character navigation

Replace the previous key handlers with the following code:

Item {    id: player    //...    QtObject {        id: flags        readonly property int speed: 100        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.isAutoRepeat) return;        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) ...

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.