4
我試圖創建兩個玩家之間的遊戲模擬。我想讓它們通過鍵盤移動,通過根矩形的Keys.onPressed屬性,但我遇到了一個問題,即兩個玩家不能同時獲得訂單,因爲只有一個關鍵事件被捕獲。是代碼:如何同時處理兩個關鍵事件
Rectangle
{
id: root
height: 1000; width: 1000
Row
{
spacing: 100
x: 350
Repeater
{
id: repeater
model: 2
Rectangle
{
width: 50
height: 100
y:500
color: "blue"
property int speedForward: 5
property int speedBackward: 3
Rectangle
{
height: 10; width: 10
x: 20
color: "red"
}
}
}
}
Keys.onPressed:
{
var robot = (event.key > 100) ? repeater.itemAt(0) : repeater.itemAt(1)
if((event.key === 16777234) || (event.key === 65))
{
robot.rotation = (robot.rotation - 5) % 360;
}
if((event.key === 16777236) || (event.key === 68))
{
robot.rotation = (robot.rotation + 5) % 360;
}
if((event.key === 16777235) || (event.key === 87))
{
robot.x = robot.x + robot.speedForward * Math.sin(Math.PI * robot.rotation/180);
robot.y = robot.y - robot.speedForward * Math.cos(Math.PI * robot.rotation/180);
}
if((event.key === 16777237) || (event.key === 83))
{
robot.x = robot.x - robot.speedForward * Math.sin(Math.PI * robot.rotation/180);
robot.y = robot.y + robot.speedForward * Math.cos(Math.PI * robot.rotation/180);
}
}
focus: true
}
有什麼想法嗎?
非常感謝你! – tzoorp
如果這個答案解決了你的問題,你應該考慮驗證它 – BlueMagma