O'Reilly logo

Game Programming using Qt 5 Beginner's Guide - Second Edition by Lorenz Haas, Witold Wysota, Pavel Strakhov

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required