一個簡單的選擇是使用millis()手動跟蹤時間。
你可以使用兩個變量:
- 一個存儲經過時間
- 一個用於存儲你需要
在你檢查draw()方法等待/延遲時間當前時間(以毫秒爲單位)與先前存儲的時間之間的差異大於(或等於)延遲。
如果是這樣,這將在你的提示做任何的延遲給出更新存儲時間:
int time;
int wait = 1000;
void setup(){
time = millis();//store the current time
}
void draw(){
//check the difference between now and the previously stored time is greater than the wait interval
if(millis() - time >= wait){
println("tick");//if it is, do something
time = millis();//also update the stored time
}
}
這裏有一個輕微的變化更新屏幕上的「針」:
int time;
int wait = 1000;
boolean tick;
void setup(){
time = millis();//store the current time
smooth();
strokeWeight(3);
}
void draw(){
//check the difference between now and the previously stored time is greater than the wait interval
if(millis() - time >= wait){
tick = !tick;//if it is, do something
time = millis();//also update the stored time
}
//draw a visual cue
background(255);
line(50,10,tick ? 10 : 90,90);
}
根據您的設置/需要,您可以選擇將類似這樣的東西包裝到可重用的類中。這是一個基本的方法,應該與Android和JavaScript版本一起工作(儘管在JavaScript中你已經有了setInterval())。
如果您有興趣使用Java的實用程序,正如FrankieTheKneeMan建議的那樣,有一個TimerTask類可用,並且我確信有很多資源/示例。
可以波紋管運行演示:
var time;
var wait = 1000;
var tick = false;
function setup(){
time = millis();//store the current time
smooth();
strokeWeight(3);
}
function draw(){
//check the difference between now and the previously stored time is greater than the wait interval
if(millis() - time >= wait){
tick = !tick;//if it is, do something
time = millis();//also update the stored time
}
//draw a visual cue
background(255);
line(50,10,tick ? 10 : 90,90);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.4.4/p5.min.js"></script>
...哪種語言? – FrankieTheKneeMan
我正在使用處理(Java) – D34thSt4lker