我正在尝试在Processing
草图中插入延迟。我试过Thread.sleep()
了,但我想它不会起作用,因为就像在 Java 中一样,它会阻止渲染图纸。
基本上,我必须绘制一个延迟绘制三个边的三角形。
我怎么做?
我正在尝试在Processing
草图中插入延迟。我试过Thread.sleep()
了,但我想它不会起作用,因为就像在 Java 中一样,它会阻止渲染图纸。
基本上,我必须绘制一个延迟绘制三个边的三角形。
我怎么做?
处理程序可以读取计算机时钟的值。使用second()函数读取当前秒,该函数返回 0 到 59 之间的值。使用minute()函数读取当前分钟,该函数也返回 0 到 59 之间的值。-处理:编程手册
其他时钟相关函数:millis()、day()、month()、year()。
这些数字可用于触发事件并计算时间流逝,如上述书中引用的以下处理草图:
// Uses millis() to start a line in motion three seconds
// after the program starts
int x = 0;
void setup() {
size(100, 100);
}
void draw() {
if (millis() > 3000) {
x++;
line(x, 0, x, 100);
}
}
这是一个三角形的示例,其边在 3 秒后绘制(三角形每分钟重置一次):
int i = second();
void draw () {
background(255);
beginShape();
if (second()-i>=3) {
vertex(50,0);
vertex(99,99);
}
if (second()-i>=6) vertex(0,99);
if (second()-i>=9) vertex(50,0);
endShape();
}
正如@user2468700 建议的那样,使用计时功能。我喜欢millis()。
如果您有一个值来跟踪特定间隔的时间和当前时间(持续更新),您可以根据延迟/等待值检查一个计时器(手动更新的)是否落后于另一个(连续的)。如果是这样,请更新您的数据(在这种情况下要绘制的点数),最后更新本地秒表之类的值。
这是一个基本的注释示例。渲染与数据更新分开,以便于理解。
//render related
PVector[] points = new PVector[]{new PVector(10,10),//a list of points
new PVector(90,10),
new PVector(90,90)};
int pointsToDraw = 0;//the number of points to draw on the screen
//time keeping related
int now;//keeps track of time only when we update, not continuously
int wait = 1000;//a delay value to check against
void setup(){
now = millis();//update the 'stop-watch'
}
void draw(){
//update
if(millis()-now >= wait){//if the difference between the last 'stop-watch' update and the current time in millis is greater than the wait time
if(pointsToDraw < points.length) pointsToDraw++;//if there are points to render, increment that
now = millis();//update the 'stop-watch'
}
//render
background(255);
beginShape();
for(int i = 0 ; i < pointsToDraw; i++) {
vertex(points[i].x,points[i].y);
}
endShape(CLOSE);
}