我的游戏需要一个计时器...... 我搜索了很多但没有运气。
请帮忙。
这是我的鼠标事件:
public void mouseClicked(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
if(shot == false){
Ink = 0;
}
if(ready == true){
shot = true;
// I need a timer here to wait a second and then stop shooting.
}
}
答案 0 :(得分:3)
再次使用Swing Timer:
// code not compiled nor tested. It was typed free-hand.
// so it was not meant to be copy, pasted and used, but rather to show you
// the idea.
public void mouseClicked(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
// don't use if (shot == false). Instead do:
if (!shot) {
Ink = 0;
}
// likewise, no need to use if (ready == true). Instead do:
if (ready) {
shot = true;
// turn off your ability to shoot here by setting a boolean.
ableToShoot = false;
// start a Swing Timer that does not repeat
// in the Timer turn back on the ability to shoot by setting a boolean
Timer swingTimer = new Timer(TIMER_DELAY_TIME, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
// allow shots here
ableToShoot = true;
}
});
swingTimer.setRepeats(false);
swingTimer.start();
}
}
注意:
Thread.sleep(...)
,因为这会睡眠Swing事件线程。