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.
IndexProgramming Questions & HelpSyntax Questions › add moving objects
Page Index Toggle Pages: 1
add moving objects (Read 338 times)
add moving objects
Sep 17th, 2006, 2:26am
 
Hi,

I started the following sketch: within a certain area ellipses are added in succession and move away instantly after they appear. The problem is, that EITHER they move away and the next ellipse isn´t drawn before the first one leaves the display window. OR every loop draws a new ellipse, the last one disappeares. The intention is to add new ellipses while the others move away undisturbed. Presumably the x-coordinate (or the ellipse-array?) has to be variable, so it won´t be extinguished by the next loop. But how could I do this?

Thanks for help!


int num = 10;
MyEllipse[] e = new MyEllipse[num];
int a = 30;
int b = 30;
int s;

void setup()
{
 size(400,400);
 
 e[1] = new MyEllipse(random(50,100),random(50,350),a,b);
 framerate (30);
 }
 
  void draw()
 {
   background (255);
   noStroke();
   fill(0,0,255,100);
   rect(50,50,100,300);
 
   int i=1; //EITHER
  //for(int i=0; i<10; i++){//OR
  //e[i] = new MyEllipse(random(50,100),random(50,350),a,b);//OR
   e[i].drawElli();
   e[i].x++;
   
   if (e[i].x-a > width)
   {
     e[i] = new MyEllipse(random(50,100),random(50,350),a,b);
    }
   // }//OR
   }
   
   void mousePressed()
   {
     if(mousePressed)
     {
       e[1] = new MyEllipse(random(50,100),random(50,350),a,b);
       }
     }
   
   class MyEllipse
   {
     float x, y, a, b;
     
     MyEllipse(float x, float y, float a, float b)
     {
       this.x = x;
       this.y = y;
       this.a = a;
       this.b = b;
       }
       
      MyEllipse(MyEllipse e)
       {
         if (e!=null)
         {
           x = e.x;
           y = e.y;
           }
         }
         
 void drawElli()
 {
   smooth();
   noStroke();
   fill(255,0,0);
   ellipse(e[1].x,e[1].y,a,b);
   }

     }
Re: add moving objects
Reply #1 - Sep 17th, 2006, 3:34pm
 
You are going to need a separate variable to keep track of total number of ellipses currently made, i.e.:

Code:

int num = 10;
MyEllipse[] e = new MyEllipse[num];
int currentTotal = 0;


Then you can run through only the available ellipses

Code:

// Accessing available ellipses
for(int i = 0; i < currentTotal; i++) {
// do stuff with the ellipses
}


. . .and know where in the array to make a new one if necessary:

Code:

// Making a new one
e[currentTotal] = new MyEllipse();
currentTotal = (currentTotal + 1) % e.length; // make sure you don't go off the end of the array


You could also investigate using an ArrayList (http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html) which is a special kind of resizable array.  Some examples here:

http://www.shiffman.net/teaching/the-nature-of-code/particles/
Re: add moving objects
Reply #2 - Sep 19th, 2006, 7:59pm
 
yippee!
Page Index Toggle Pages: 1