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 › new object, new variables
Page Index Toggle Pages: 1
new object, new variables? (Read 278 times)
new object, new variables?
Jun 23rd, 2008, 1:14pm
 
Hello,

i'm trying to make an ellipse, and after a while, there will be another ellipse. A sort of radar.
But my question is:
how can i create a new object with the same, but new variables. So that each object has it's own size.

Code:

cirkel c1 = new cirkel(0,0);
int i = 0;

void setup(){
size(600,600);
noFill();
smooth();
strokeWeight(2);
}

void draw(){
background(255);
c1.update();
}

class cirkel{
float xpos,ypos;
cirkel(float x, float y){
xpos = x;
ypos = y;
}
void update(){
xpos = xpos + 1;
ypos = ypos + 1;
ellipse(mouseX,mouseY,xpos,ypos);
if(xpos>=200){
i = i + 1;
cirkel i = new cirkel(0,0);
}
}
}



thanks
Re: new object, new variables?
Reply #1 - Jun 23rd, 2008, 2:03pm
 
The redefinition of i (first integer, then object) is messy, at least... Plus the new object will die as soon as it is created.

I am not too sure of what you try to do. Concentric circles?
If so, create an array of cirkel objects:

Code:
int i = 0;
int MAX_C = 10; // Adjust
cirkel[] cl = new cirkel[MAX_C];

void draw() {
if ((i == 0 || cl[i - 1].getWidth() > 200) && i < MAX_C) {
cl[i++] = new cirkel(0, 0);
}
for (int k = 0; k < i; k++) {
cl[k].update();
}
}

class cirkel() {
[...]
void getWidth() {
return xpos;
}
}

and drop the test and object creation in update().
I prefer to keep the control on the collection of classes outside of the class itself (or make a class for the collection).

Not tested...
Page Index Toggle Pages: 1