我的游戏循环中有这段代码:
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
if (aPress) {
rotate -= rotSpd;
rotate = rotate < 0 ? 360 + rotate : rotate;
}
if (dPress) {
rotate += rotSpd;
rotate = rotate > 360 ? rotate - 360 : rotate;
}
if (wPress) {
x += (rotate < 180 ? speed : -speed) * Math.abs(Math.sin(Math.toRadians(rotate)));
y += (rotate < 270 ? (rotate < 90 ? -speed : speed) : -speed) * Math.abs(Math.cos(Math.toRadians(rotate)));
}
if (sPress) {
x -= (rotate < 180 ? speed : -speed) * Math.abs(Math.sin(Math.toRadians(rotate)));
y -= (rotate < 270 ? (rotate < 90 ? -speed : speed) : -speed) * Math.abs(Math.cos(Math.toRadians(rotate)));
}
repaint();
try {
Thread.sleep(20);
} catch (InterruptedException annoyingUncheckedException) {}
}
}
}).start();
它做了它应该做的事情:当你按下 A 时,它会逆时针转动。当你按下 D 时,它会顺时针转动。按 W 时前进,按 S 时后退。但是,如果我按住 W 和 D,起初它会像它应该的那样绕一个圆圈,但它会慢慢开始向左上角的方向移动。我怎样才能解决这个问题?