Your app behaves the way you told it to. If you press a key, you get a key event with that key in the event's key property. Then you get more events for that key because of autoRepeat being active, hence your event handlers fire again. There is no logic in your code for handling keys which are already pressed when a new event arrives. A regular approach to handling key events in games is to have a set of flags which are raised or lowered based on incoming events.
SomeElement {
id: root
// ...
property bool isMovingRight: false
Keys.onRightPressed: {
if(event.isAutoRepeat) return
isMovingRight = true
}
Keys.onReleased: {
if(event.isAutoRepeat) return
if(event.key === Qt.Key_Right) isMovingRight = false
}
states: [
State {
name: "characterMovingRight"
when: root.isMovingRight
}
]
}
SomeElement {
id: root
// ...
property bool isMovingRight: false
Keys.onRightPressed: {
if(event.isAutoRepeat) return
isMovingRight = true
}
Keys.onReleased: {
if(event.isAutoRepeat) return
if(event.key === Qt.Key_Right) isMovingRight = false
}
states: [
State {
name: "characterMovingRight"
when: root.isMovingRight
}
]
}
To copy to clipboard, switch view to plain text mode
By the way:
I know that QML is not the best tool for building games in
Where exactly do you know that from? My impression is exactly opposite to that of your source of information.
Bookmarks