文章目錄
  1. 1. Introduction
  2. 2. To think of it
  3. 3. To do this

Introduction

之前一直想实现“技能冷却”效果,百度和论坛上一时间也搜不到什么好的方法,在宿舍和天花板交流了一会以后发现用一个简单的计时器即可。

To think of it

我要使用一个技能fff();这个技能需要放在一个布尔锁里,每当使用这个技能,就用一个计时器去控制布尔锁。

To do this

首先要引入Timer类,调用Timer里的schedule方法,这个方法需要引进一个TimerTask类参数,和下一次执行的时间。

运行环境:Processing-Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.util.Timer;
boolean fA = true; //fireAvailable
Timer timer;
//这里的draw()用来即时激活keyPressed()函数
void draw() {
}
void keyPressed() {
fff();
}
//有汽油就烧!
void fff() {
if (key == 'f') {
if (fA) {
println("FFFFFF!");
setTimer();
} else println("Fire is not prepared, please wait 5s.");
}
}
//待我去买汽油
void setTimer() {
fA = false;
Timer timer = new Timer();
timer.schedule(new task(), 5000L);
}
//建立一个买汽油的任务
class task extends java.util.TimerTask {
void run() {
fA = true;
}
}

运行结果

FFFFFF!
Fire is not prepared, please wait 5s.
Fire is not prepared, please wait 5s.
Fire is not prepared, please wait 5s.
Fire is not prepared, please wait 5s.
Fire is not prepared, please wait 5s.
Fire is not prepared, please wait 5s.
Fire is not prepared, please wait 5s.
Fire is not prepared, please wait 5s.
Fire is not prepared, please wait 5s.
FFFFFF!
Fire is not prepared, please wait 5s.
Fire is not prepared, please wait 5s.
Fire is not prepared, please wait 5s.
Fire is not prepared, please wait 5s.
Fire is not prepared, please wait 5s.

完美

文章目錄
  1. 1. Introduction
  2. 2. To think of it
  3. 3. To do this