2012-02-09 39 views

回答

4

不能說它的效率,但我想一個辦法是將舊的位置保留在ArrayList中?然後,您可以在每個點之間繪製線條,只要您在每個幀上按下當前位置並刪除最近的點。希望能幫助到你!

PVector circlePosition; 
ArrayList<PVector> circleTrail; 
int trailSize = 10; 

void setup() { 
    size(500, 500); 
    circlePosition = new PVector(width*0.5, width*0.5); 
    circleTrail = new ArrayList<PVector>(); 
} 

void draw() { 
    background(255); 
    int trailLength; 

    circlePosition = new PVector(mouseX, mouseY); 
    circleTrail.add(circlePosition); 

    trailLength = circleTrail.size() - 2; 
    println(trailLength); 

    for (int i = 0; i < trailLength; i++) { 
    PVector currentTrail = circleTrail.get(i); 
    PVector previousTrail = circleTrail.get(i + 1); 

    stroke(0, 255*i/trailLength); 
    line(
     currentTrail.x, currentTrail.y, 
     previousTrail.x, previousTrail.y 
    ); 
    } 

    ellipse(circlePosition.x, circlePosition.y, 10, 10); 

    if (trailLength >= trailSize) { 
    circleTrail.remove(0); 
    } 

} 
4

不能以我的方法是有效的說話,但我做的方式是通過每次繪製一個矩形在你的整個草圖與也設置爲較低的值(像25個左右)。這會導致以前draw()循環中的對象看起來「褪色」。例如:

int i = 0; 

void setup(){ 
    size(500,500); 
    smooth(); 
    noStroke(); 
    background(255); 
} 

void draw(){ 
    fill(255,25); 
    rect(0,0,width,height); 
    fill(0); 
    ellipse(width/2 + i,height/2 + i,50,50); 
    delay(100); 
    i+=10; 
}