在下面的代码中是一个在用户点击GUI中的JLabel时调用的方法。该方法通过pos []整数列表中的coördinates传递,并且'player'的当前位置由m.findPosition()访问,m.findPosition()也返回整数列表。除了启动的计时器之外,方法内的所有内容都能正常工作。计时器内的代码不会执行,其他一切都会执行。我还注意到,在我点击JLabel之后,没有注册Keyboard和Mouse事件。我的直觉说计时器确实被执行但不知何故里面的代码没有?
先谢谢你们!
PS:我还试图将Try& Catch放在它周围,但我没有遇到任何错误。
注意:.moveMatrix()函数采用一个方向('l'是左,'u'等),然后将该方向的对象移动1.这四种情况(必须向上或向下移动必须向左或向右移动)在这里检查,移动到某个位置。定时器应该不断重复,直到每个坐标的实际位置和所需位置(pos [])之间的差值为0.
int timertime = 500;
int[] officialpos = m.findPosition();
System.out.println(officialpos[0] + "," + officialpos[1]);
System.out.println(pos[0] + "," + pos[1]);
int dx = pos[1] - officialpos[1];
int dy = pos[0] - officialpos[0];
System.out.println(dx);
System.out.println(dy);
if (dy > 0) {
Timer time_down = new Timer(timertime, new ActionListener() {
public void actionPerformed(ActionEvent ae) {
System.out.println("test");
m.moveMatrix('d');
updateMatrix(m, true);
System.out.println(m.findPosition()[1] + "," + m.findPosition()[0]);
}
});
time_down.start();
while (pos[0] - m.findPosition()[0] > 0) {
}
time_down.stop();
} else if (dy < 0) {
System.out.println("dy < 0, dus omhoog");
Timer time_up = new Timer(timertime, new ActionListener() {
public void actionPerformed(ActionEvent ae) {
System.out.println("test");
m.moveMatrix('u');
updateMatrix(m, true);
System.out.println(m.findPosition()[1] + "," + m.findPosition()[0]);
}
});
time_up.start();
while (pos[0] - m.findPosition()[0] < 0) {
}
time_up.stop();
}
if (dx > 0) {
System.out.println("dx > 0, dus naar rechts");
Timer time_right = new Timer(timertime, new ActionListener() {
public void actionPerformed(ActionEvent ae) {
System.out.println("test");
m.moveMatrix('r');
updateMatrix(m, true);
System.out.println(m.findPosition()[1] + "," + m.findPosition()[0]);
}
});
time_right.start();
while ((pos[1] - m.findPosition()[1]) > 0) {
}
time_right.stop();
} else if (dx < 0) {
System.out.println("dx < 0, dus naar links");
Timer time_left = new Timer(timertime, new ActionListener() {
public void actionPerformed(ActionEvent ae) {
System.out.println("test");
m.moveMatrix('l');
updateMatrix(m, true);
System.out.println(m.findPosition()[1] + "," + m.findPosition()[0]);
}
});
System.out.println("starting timer");
time_left.start();
System.out.println("Timer started");
while (pos[1] - m.findPosition()[1] < 0) {
// System.out.println("waiting");
}
time_left.stop();
}