[英]How can I apply an actionlistener to my timer?
I am trying to link an actionlistener to a timer for a game I am writing. 我正在尝试将actionlistener链接到我正在编写的游戏的计时器。 Whenever the timer fires, monsters in a 2D array randomly move to one adjacent tile.
每当计时器触发时,2D阵列中的怪物随机移动到一个相邻的图块。 The array is not completely full of monsters.
阵列并没有完全充满怪物。 Where there aren't monsters, my array has null.
在没有怪物的地方,我的数组为null。
Here's what I have so far: 这是我到目前为止所拥有的:
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();
}
}
And inside my Monster class: 在我的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;
}
When I create my timer, I do this 当我创建我的计时器时,我这样做
public final int DELAY = 1000;
Timer myTimer = new Timer(DELAY, new MonsterListener());
However, I keep getting an error that reads 但是,我一直收到一个错误
cannot find symbol
symbol : constructor Timer(int,Game.MonsterListener)
location: class java.util.Timer
myTimer = new Timer(DELAY, listener);
^
I think you need to use javax.swing.Timer
instead of java.util.Timer
我认为你需要使用
javax.swing.Timer
而不是java.util.Timer
[I suspect this because you are using ActionListener
] [我怀疑这是因为你正在使用
ActionListener
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.