2016-09-20 65 views
1

我想淡入淡出背景進入不同的顏色。我決定嘗試繪製一個矩形而不是使用背景,但是我的粒子越來越小,因爲背景沒有被繪製。如何解決這個問題的建議?顏色改變處理w/out影響粒子的背景

// main tab 
ArrayList<ParticleSystem> systems; 

PVector windRight = new PVector(0.1,0); 
PVector sortaSpeed = new PVector(-0.1,0); 
PVector gravity = new PVector(0,0.05); 

boolean wR = false; 
boolean sP = false; 
boolean cS = false; 

int limit = 8; 

int alpha = 10; 
color[] colorArray = {color(0,0,0,alpha),color(16, 37, 43,alpha),color(51, 10, 10,alpha),color(126, 255, 0,alpha)}; 
int currentColor; 
int nextColor; 

boolean change = false; 

void setup() { 
    size(640,480); 
    systems = new ArrayList<ParticleSystem>(); 
    smooth(); 
    noStroke(); 
    currentColor = 0; 
    for(int i = 0; i < limit; i++){ 
    systems.add(new ParticleSystem(random(100,200),10,new PVector(random(100,500),-5))); //random(480) 
    } 
    background(0); 
} 

void draw() { 
    //rect(0, 0, 2000, 2000); 
    fill(colorArray[currentColor]); 
    rect(0, 0, width*2, height*2); 
    //background(colorArray[currentColor]); 
    if(change){ 
     currentColor = nextColor; 
     change = false; 

    } 

    if(!systems.isEmpty()){ 
     for (int i =0; i < systems.size(); i++){ 
     ParticleSystem ps = systems.get(i); 
     ps.applyForce(gravity); 
     ps.run(); 
     if(wR){ 
      ps.applyForce(windRight); 
     } 
     if(sP){ 
      ps.applyForce(sortaSpeed); 
     } 
     } 
    } 
} 

void keyPressed() { 

    if(key == 'w'){ 
    wR = true; 
    print("w"); 
    } 
    if(key == 'a'){ 
    print('a'); 
    sP = true; 
    } 
} 

void keyReleased(){ 
    if(key == 'w'){ 
    wR = false; 
    } else if(key == 'a'){ 
    sP = false; 
    } else if(key == 's'){ 
    if(key == 's'){ 
     change = true; 
     println("currentColor: "+currentColor); 
     int newColor = currentColor; 
     while (newColor == currentColor) 
     newColor=(int) random(colorArray.length); 
     nextColor = newColor; 
    } 


    } // end of cS 
} 

回答

0

在未來,請嘗試發佈的MCVE。現在我們無法運行您的代碼,因爲它包含編譯器錯誤。無論如何,我們不需要看到你的整個草圖,只是一個小例子而已。

但是,您的問題是由於您嘗試在背景中使用Alpha值導致的。這對background()函數不起作用,因爲該函數會忽略alpha值,並且它不會與rect()函數一起工作,因爲那樣您將不會清除舊幀 - 您將通過透明矩形看到它們。

取而代之,您可以使用lerpColor()函數來計算從一種顏色到另一種顏色的轉換。

這裏是一個隨機顏色之間過渡的例子:

color fromColor = getRandomColor(); 
color toColor = getRandomColor(); 

float currentLerpValue = 0; 
float lerpStep = .01; 

void draw() { 

    color currentColor = lerpColor(fromColor, toColor, currentLerpValue); 

    currentLerpValue += lerpStep; 
    if (currentLerpValue >= 1) { 
    fromColor = currentColor; 
    toColor= getRandomColor(); 
    currentLerpValue = 0; 
    } 

    background(currentColor); 
} 

color getRandomColor() { 
    return color(random(255), random(255), random(255)); 
}