2013-05-14 3 views
0

이 그룹의 마지막 배열이 추가 될 때까지 어레이 그룹이 페이드 아웃되게하고 싶습니다. 예를 들어, 나는 zoog [0], zoog [1], zoog [2]를 만들기 위해 append를 사용하고, zoog [2]가 생성 될 때까지이 세 객체를 fadeout하지 않고 잠시 기다려야한다. 3], zoog [4], zoog [5]와 같이이 세 객체는 zoog [5]가 생성 될 때까지 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; 
//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++){ 
// if(millis()-thistime>4000){ 
//  zoog[i].disappear(); 
// } 
    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(xpos,ypos,message,nopacity); 
zoog =(Zoog[]) append(zoog,b); 

} 

난 당신이 3 zoogs을하고 그들이 사라지고있어 아웃 할 때까지 그 세 페이딩 시작하려면 제대로 이해하고 Zoog 클래스

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

다른 배열을 설치해야하는 것처럼 보입니다 – Douuga

+0

추가하지 말고 ArrayList를 사용해야합니까? – Douuga

+0

문제는 Zoog [0], Zoog [1], Zoog [2]가 Zoog [2]가 생성 될 때만 함께 사라지 길 원합니다. – Douuga

답변

3

. 이것이 맞다면이 작업을 수행하는 몇 가지 방법이 있습니다.

먼저 동적으로 금액을 업데이트하는 경우 배열을 사용하지 않겠습니다. 네가하고 싶다면 arraylists. Here's javadocs 참조. 기본적으로 Zoogs의 arraylist를 초기화하고 zoog.add (새로운 Zoog ...)를 mousepressed에 넣습니다. arraylists에 대한 좋은 점은 당신이 그들을 조작 할 수있는 많은 멤버 함수를 가지고 있다는 것입니다. 예를 들어 시간 대신에 그리기 기능에서 arraylist의 크기를 확인할 수 있습니다. 일단 3을 초과하면 처음 3 개가 죽을 때까지 페이드 아웃을 시작합니다 (Zoog 멤버 함수를 사용하여 죽었다고 말합니다). 당신은 draw 루프에서 "isDead"멤버 함수를 체크하고 for 루프에서 올바른 죽은 Zooog를 제거 할 수 있습니다.

다음은 단지 불투명도가 큰 0보다 작은 지 여부를 반환하여 Zoog 클래스의 isDead 기능을 만들어 가정, 거친 구현의 :이 더 완벽한 예는 아니다

void Draw() 
{ 
    for (Zoog zoog : zoogs) //for each statement simplifying the code -  
          //says for each Zoog in zoogs do 
    { 
     zoog.jiggle(); 
     zoog.display(); 
    } 

    if(zoogs.size() >= 3) {  
     for(int i = 0; i < 3; i++) { 
     zoogs.get(i).disappear(); 
     } 
    } 

    if (zoogs.get(0).isDead() && zoogs.get(1).isDead() && zoogs.get(2).isDead()) { 
      zoogs.remove(2); 
      zoogs.remove(1); 
      zoogs.remove(0); 
    } 
} 

을하지만 방법을 보여줍니다 그들의 불투명도를 줄이고 그들이 죽었는지 확인함으로써 한번에 3 마리의 동물원을 제거하십시오. 백만 번 클릭하면 각 체인이 죽을 때까지 어느 정도 시간이 걸릴 것입니다.