我正在尝试将 Action 监听器链接到我正在编写的游戏的计时器。每当计时器触发时,二维阵列中的怪物就会随机移动到相邻的一个方 block 。阵中并没有完全充满怪物。如果没有怪物,我的数组就为空。
这是我到目前为止所拥有的:
private class MonsterListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < Level.SIZE; i++) {
for (int j = 0; j < Level.SIZE; j++) {
if (monsters[i][j] != null) {
monsters[i][j].update();
}
}
}
updateState();
}
}
在我的 Monster 类中:
public void update() {
rand1 = new Random();
rand2 = new Random();
drow = rand1.nextInt(3); //random int 0,1,2
dcol = rand2.nextInt(3); //random int 0,1,2
drow -= 1; //random int -1,0,1
dcol -= 1; //random int -1,0,1
row += drow;
col += dcol;
}
当我创建计时器时,我会这样做
public final int DELAY = 1000;
Timer myTimer = new Timer(DELAY, new MonsterListener());
但是,我不断收到错误消息
cannot find symbol
symbol : constructor Timer(int,Game.MonsterListener)
location: class java.util.Timer
myTimer = new Timer(DELAY, listener);
^
最佳答案
我认为您需要使用javax.swing.Timer
而不是java.util.Timer
[我怀疑这是因为您正在使用 ActionListener
]
关于java - 如何将 Action 监听器应用到我的计时器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13572226/