How it works...

What we did here was constantly add the moveX and moveZ value's movement vector's x and z values. When a key is pressed, moveX and moveZ will become a positive or negative number, depending on which button is pressed; otherwise, it will be zero. In the keyPressEvent() function, we checked whether the keyboard button that was pressed was W, A, S, or D; we then set the variables accordingly. To get the full list of key names that are used by Qt, visit http://doc.qt.io/qt-5/qt.html#Key-enum.

One way we can do the movement input is by holding down the same key and not releasing it. Qt 5 will repeat the key press event after an interval, but it is not very fluid as modern operating systems limit the key press event to prevent double typing. The keyboard input interval varies between different operating systems. You can set the interval by calling QApplication::setKeyboardInterval(), but this may not work on every operating system. Therefore, we did not go for this method.

Instead, we only set moveX and moveZ once when the key was pressed or released, and then we constantly apply the value to the movement vector in our game loop so that it is moving continuously without being affected by the input interval.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset