我想让一组数组淡出,直到添加了该组中的最后一个数组对象。我使用millis() 使每三个对象以较慢的速度淡出!所以我创建了一个名为的函数boolean timelag(int time, int number)
,每次我将时间和序列号传递给它,并期望它会在每三个对象创建后 2 秒后淡出,但似乎什么也没发生
void draw() {
background(255, 255, 255);
for (int i=0; i<zoog.length; i++) {
zoog[i].jiggle();
zoog[i].display();
if(i%3 ==0 && i>=3){
time = millis();
timelag(time,i);
}
}
if(fadeout){
zoog[thatnumber].disappear();
zoog[thatnumber-1].disappear();
zoog[thatnumber-2].disappear();
}
}
我的延时功能:
boolean timelag(int time, int number){
int thattime = time;
if(millis()-thattime>2000){
thatnumber = number;
fadeout = true;
}
else
fadeout = false;
return fadeout;
}
整个代码在这里
Zoog[]zoog = new Zoog[1];
float count=0;
int xpos =0;
int ypos =0;
String message="haha";
int ntextsize = 20;
int nopacity =200;
int thistime = 0;
int thiscount = 0;
int time =0;
int number =0;
boolean fadeout = false;
int thatnumber=0;
//Zoog zoog;
void setup() {
size(400, 400);
xpos = int(random(width/2-200, width/2+40));
ypos = int(random(height/2, height/2-40));
zoog[0] = new Zoog(xpos, ypos, message, nopacity);
}
void draw() {
background(255, 255, 255);
for (int i=0; i<zoog.length; i++) {
zoog[i].jiggle();
zoog[i].display();
if(i%3 ==0 && i>=3){
time = millis();
timelag(time,i);
}
}
if(fadeout){
zoog[thatnumber].disappear();
zoog[thatnumber-1].disappear();
zoog[thatnumber-2].disappear();
}
}
void mousePressed() {
count = count + 1;
// int thiscount = 0;
if (count%3 ==0) {
xpos=int(random(30, width-30));
ypos=int(random(10, height-10));
}
else {
ypos = ypos+50;
}
nopacity = int(random(100, 255));
// text(message, xpos, ypos);
Zoog b = new Zoog(xpos, ypos, message, nopacity);
zoog =(Zoog[]) append(zoog, b);
}
boolean timelag(int time, int number){
int thattime = time;
if(millis()-thattime>2000){
thatnumber = number;
fadeout = true;
}
else
fadeout = false;
return fadeout;
}
class Zoog {
int x;
int y;
String thatmessage;
int opaci =0;
Zoog(int xpo, int ypo, String thismessage, int opa) {
x = xpo;
y = ypo;
thatmessage = thismessage;
opaci = opa;
}
void jiggle() {
x = x+int(random(-2, 2));
y = y+int(random(-2, 2));
}
void display() {
fill(0, opaci);
text(thatmessage, x, y);
print("x position is "+ x);
print("y position is "+y);
}
void disappear() {
for (int j=0; j<255; j++) {
opaci = opaci -j;
}
}
}