Mmm, I did this little sketch to show you how to use it. I´ve added it some stuff because quarks is absolutely right. One thing is to "kill" an object cleaning the reference and other one is the job of the garbage collector. If you check the sketch you will see there´s a method called finalize() on the class Cloud. Finalize() method is useful if you want an object to perform a task before being collected by the GC. In this case finalize() simply tells us an object has been collected. If you don´t include this line "if(k>=20){System.gc(); k=0;}" you will see that although the list contains lesser objects as time goes by, the garbage collector is doing nothing at all. So it´s a nice idea to call the GC when you reach a significative level of memory trash to clean. In this case 20, but I´d set that level to higher numbers... all depending on how memory costs each object Cloud to the heap. It´s not a good idea to call it often, I think, only as it becomes necessary.
You can also try what byhring said, it is also a good idea. You only have to think how to deal with the exceptions in a simple array... but that´s not tough... ;-)
LinkedList <Cloud> clouds;
int cloudsNumber=50;
float alphaLimit=.5;
int k=0;
void setup(){
size(500,500);
background(-1);
smooth();
clouds = new LinkedList<Cloud>();
for (int i=0; i<cloudsNumber;i++) { clouds.add(new Cloud()); }
}
void draw(){
for (int i=0; i<clouds.size(); i++) {
Cloud c= clouds.get(i);
c.render();
c.update();
if(c.alpha<5) {clouds.remove(c); println(clouds.size());}
}
if(k>=20){System.gc(); k=0;}
}
class Cloud {
float alpha,x,y,R;
Cloud(){
alpha=random(255);
x=random(width);
y=random(height);
R=100;
}
void render(){
fill(0,alpha);
stroke(-1,alpha);
ellipse(x,y,R,R);
}
void update(){
alpha-=.5;
R-=.5;
}
void finalize(){ println("Garbage collector took one more"); }
}
Ale ·
http://60rpm.tv/i