We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
free array (Read 601 times)
free array
Aug 5th, 2009, 12:17pm
 
Hi,

I trained a little bit with dynamic object generating. When I draw a line, sometimes drops move down, starting from the saved mouse position. This works perfectly:

Code:

(...)
if (mouseButton == LEFT) {
   // normal spray drawing foo
   stroke(pencil, dropAlpha);
   strokeWeight(20);
   line(pmouseX, pmouseY, mouseX, mouseY);

   // drop generating
   int randomValueForDrop = floor(random(0, 100));
   if (randomValueForDrop <= randomDrop) {
     ColorDrop setDrop = new ColorDrop(mouseX, mouseY, pencil);
     drop = (ColorDrop[]) append(drop, setDrop);
   }
(...)


well ... this is for the case, that I draw color lines. but I also want to reset the drawing field with the right mouse button. So I use background(0) which works for the drawn lines from the mouse, but the generated drops still move until they reach their max position.

How can i delete all objects from my object array "drop"?
Re: free array
Reply #1 - Aug 5th, 2009, 12:54pm
 
You can simply just stop to draw them (maintain a boolean variable, and if true, don't draw).
Or use shorten() to clear the array?
Re: free array
Reply #2 - Aug 5th, 2009, 1:29pm
 
stop drawing must not be used, because the array would grow to infinity by time ... but I think I got it! I'll try "ArrayList" instead of a normal array, because I can write e.g. drop.remove[i] ... be back in a moment Wink

[UPDATE]
Shocked ArrayList doesn't work as I thought ... the function remove() doesn't work ... instead, I switched back to normal Arrays and created a new object called delDrops with zero elements ... when I want to reset the drops I only write:
Code:

drop = delDrops;

Undecided I don't get the point why this was so difficult ... maybe I should get more sleep
Re: free array
Reply #3 - Aug 5th, 2009, 4:13pm
 
You can stop drawing and appending stuff...

But you are right, using ArrayLists is better for your needs.
remove() does work, perhaps you use it incorrectly. Note you have also clear().
And you can also nullify the array: drop = null;
Page Index Toggle Pages: 1