2013-05-20 4 views
0

mousePressed()가 3 초마다 false 일 때 화면에 텍스트 표시를 만들고 싶습니다. mousePressed() 함수에서 "여부"를 부울로 설정하고 문자열을 false로 만들면 문자열을 가져옵니다. 텍스트 파일에서. 하지만 내 논리가 잘못된 것처럼 보입니다. 문제를 아는 사람이 있습니까?mousePressed()가 거짓 일 때 텍스트 추가

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

} 
+0

하지,하지만 당신은해야 설치()에서 텍스트 파일을 한 번로드하십시오. –

답변

0

그냥 항상 true가됩니다 if(whether!=true)에 대한 테스트하기 전에 whether = true를 호출하고 테스트가 아니므로 true로 평가 될 것이므로 블록 안의 코드는 실행되지 않습니다.

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

참조하십시오의 mousePressed()는 그래서 어떻게 작동하는지 이해 마우스를 누를 때, 한 번만 호출이 코드를 실행? 그러나 필요에 따라 동작하는 mousePressed (괄호 안 함)라는 필드에는 처리중인 필드 ("기본"var)가 있습니다. 동일한 코드를 실행하면 draw에서 테스트 할 수 있습니다. mousePressed 필드를 추가하면 draw가 표시됩니다. 평균 :

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 
    } 
관련 문제