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 › Two shapes in a single class
Page Index Toggle Pages: 1
Two shapes in a single class (Read 504 times)
Two shapes in a single class
Feb 27th, 2009, 1:36pm
 
I'm loading a class into an array so that, when triggered, two shapes will generate with a shared and similar set of variables (a rect() and an ellipse() with the same x and y, for example).

My guess is that I declare all the ints and floats I need in the class definition, and then in void display(), I list both the shapes like so:

void display () {
  rect(x, y, h, w);
  ellipse(x, y, d, d);
}

Even if I'm correct there, how do I call the class with only one line (or two, if needed), and how to I keep the variables private to each instanced pair of rectangle and ellipse?

Much obliged for any help, folks - you've been spot on for me so far.
Re: Two shapes in a single class
Reply #1 - Feb 27th, 2009, 2:10pm
 
Your guess is good... Smiley
But I don't get the "call the class". Perhaps you mean create a new instance? With something like:

CompoundShape cs = new CompoundShape(x, y, h, w, d);

for example.
And each instance have its own set of variables, independent of each other instance (unless you declare them static, which is another story and not possible in a .pde file anyway).
That's the base idea of objects... Smiley
Re: Two shapes in a single class
Reply #2 - Feb 27th, 2009, 3:45pm
 
Ah, so if the class definition outlines the order like so:

Class(int xpos, int ypos, int hgt, int wdt, int dia); {
... // defining each of them...
}

And if the display void comes out like so:

void display() {
  rect(x, y, h, w);
  ellipse(x, y, d, d);
}

And if the function generating the class uses the same order of variables like so:

Class test = new Class(xvar, yvar, hvar, wvar, dvar);

It will generate both the rect() and the ellipse() using the shared variables?

If so, you just saved my bacon!
Page Index Toggle Pages: 1