以较低的速度淡出文本

时间:2013-05-15 20:59:34

标签: java text processing fade

我想让一组数组淡出,直到附加了该组中的最后一个数组对象。我使用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(); 
    }
}

我的timelag功能:

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;
    }
  }
}

2 个答案:

答案 0 :(得分:2)

我假设你写的时候......

if(fadeout) { ... }
你的意思是......

if(timelag()) { ... }

在你的timelag函数中,它更可读,更快(甚至是微不足道)从函数返回true或false而不是返回变量,除非整个项目需要反复使用该变量,它不会看起来好像它是否是更改它的函数通常不需要返回值,除非你检查更改是否发生的布尔值。

boolean timelag(int time, int number){
  //int thattime = time; //You also don't need to create this you 
  //can simply use the time you're getting in the boolean statement
  if(millis()-time>2000){
    thatnumber = number;
    return true;
  }
  else {
    return false;
  }
}

另外,如果您正在尝试修复每个渐变淡出所需的时间,则需要为它们提供所有数字,然后在每次调用消失函数时减少该数字。从消失中取出for循环,让它在绘制循环中从每次调用中减去一个单位。

void disappear() {
   opacity -= somenumber //somenumber is usually something small and you can tweak it. 
   if (opacity == 0) {
      dead = true;
   }
}

您可以将绘制循环视为for循环。如果你嵌入太多单数for循环,它会减慢程序的流量。现在,你可能甚至看不到它们逐渐消失,你可能不会像现在编写代码一样摆脱它们。

当你进行测试时,你可以调整这个数字,直到找到最佳位置。如果您想要了解所有这些概念,可以查看here。 Shiffman真的深入到我们在这里讨论的各个方面,阅读起来很简短有趣。

答案 1 :(得分:1)

我第一次读到另一篇文章时,我误解了你的目标。无论如何,我已经在你的代码中做了一些调整,可能有助于你理解前进的方向。但我没有将它移动到ArrayList,所以下面这段代码,有点糟糕......它只能,也许,可以帮助你把事情弄清楚......

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;
//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);

  for (int i=0; i<zoog.length; i++) {
    zoog[i].jiggle();
    zoog[i].display();  }
}




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;
    //   thiscount = thiscount +1;
    //   thistime = millis();
    //  }
  }


  nopacity = int(random(100, 255));
  text(message, xpos, ypos);
  Zoog b = new Zoog(mouseX, mouseY, message, nopacity);
  zoog = (Zoog[]) append(zoog, b);

  zoog[zoog.length -2].disappear = true;
}





class Zoog {
  int x;
  int y;
  String thatmessage;
  boolean disappear;

  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() {
    if(disappear)
    disappear();
    fill(0, opaci);
    text(thatmessage, x, y);
  }

  void disappear() {

    opaci-=0.5;

  }
}