当mousePressed()为false时附加文本

时间:2013-05-20 12:08:30

标签: java arrays processing

我希望当mousePressed()每3秒为假时在屏幕上显示文本,我在mousePressed()函数中设置了一个布尔“是”,当它为quals false时,我从文本文件中获取字符串。但似乎我的逻辑错了。有人知道这个问题吗?

Zoog[]zoog = new Zoog[1];
float count=0;
int xpos =0;
int ypos =0;
String message="haha";
String newone="";
String t="\n";
int ntextsize = 20;
int nopacity =200;
int thistime = 0;
int thiscount = 0;
String[]lines;
//Zoog zoog;
boolean whether;

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++){
//    if(millis()-thistime>4000){
//     zoog[i].disappear(); 
//    }
    zoog[i].jiggle();
    zoog[i].display();


  }
  whether = false;
  lines = loadStrings("data.txt");
  if(whether!=true){
  createnew(int(random(width)), int(random(height)), lines[int(random(lines.length))],150);
  }
}


void mousePressed(){
  whether = true;
   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));

 createnew(xpos,ypos,message,nopacity);

}

void createnew(int xxpos, int yyos, String mmessage, int nnopacity){

  Zoog b = new Zoog(xpos,ypos,message,nopacity);
 zoog =(Zoog[]) append(zoog,b);

}

与我的问题对应的功能是:

 lines = loadStrings("data.txt");
  if(whether!=true){
  createnew(int(random(width)), int(random(height)), lines[int(random(lines.length))],150);
  }
}

void createnew(int xxpos, int yyos, String mmessage, int nnopacity){

  Zoog b = new Zoog(xpos,ypos,message,nopacity);
 zoog =(Zoog[]) append(zoog,b);

}

1 个答案:

答案 0 :(得分:0)

当您在whether = true中测试它之前调用if(whether!=true)时,它将始终为true并且测试不会评估为true,因此块内的代码将不会运行。按下鼠标时调用mousePressed()一次,运行此代码以了解其工作原理:

boolean b;
void setup(){frameRate(10);}
void draw(){ b = false; println("in draw " + b);}
void mousePressed(){b = true;println("in mousePressed " + b);}

见?但是在处理过程中有一个字段(一个“默认”var)叫做mousePressed(没有括号),就像你需要的那样,只需在绘图中测试它,运行相同的代码,在绘图中添加mousePressed字段会告诉你我是什么意味着:

boolean b;

void setup() {
  frameRate(10);
}

void draw() { 
  b = false; 
  println("in draw " + b); 
  if (mousePressed)println("i'm pressed");
}

void mousePressed() {
  b = true;
  println("im mousePressed " + b);
}

您还可以创建自己的布尔值,并在mousePressed()中将其设置为true,并在mouseReleased()中将其设置为false。具有相同的效果。

[编辑]刚刚发生在我身上,还有另外一种方式......如果您按照以下方式更改抽签中的电话,它也应该有效:

 lines = loadStrings("data.txt");
  if(whether!=true){
  createnew(int(random(width)), int(random(height)), lines[int(random(lines.length))],150);
   whether = false;// move this here
  }