Here is my class in Processing ...
class Stripe {
PShape s;
float x, y, w;
static Stripe lastStripe;
Stripe() {
x = width / 2;
y = 0;
w = 150;
s = createShape();
s.beginShape();
s.fill(0);
s.noStroke();
s.vertex(w, 0);
s.bezierVertex(w + 50, height * 1/4, w - 50 , height * 1/3, w, height);
s.vertex(0, height);
s.bezierVertex(-50, height * 1/3, 50 , height * 1/4, 0, 0);
//s.vertex(0, 0);
//s.bezierVertex(50, 50, 50, 50, 100, 100);
s.endShape(CLOSE);
}
void draw() {
pushMatrix();
translate(x, y);
shape(s);
popMatrix();
}
void move() {
x = x + STRIPE_VEL;
if (x > (width + OFFSCREEN_BUFFER)) {
x = Stripe.lastStripe.x - STRIPE_SPACING;
Stripe.lastStripe = this;
}
}
}
When I try and compile I get the following error ...
The field lastStripe can only be declared static; static fields can only be declared in static or top level types
Looking at this Java tutorial, this seems to be a valid Java pattern. Although the code above is self referential, it still raises the same error if the type is changed to an int
or similar.
What is the problem here?
EDIT: By request, here is the rest of the sketch which is in another tab. I'm starting to think that the Processing way is to just declare such variables as a 'global' rather than a static variable on a class ... I'll probably just do that.
float STRIPE_VEL = 0.5;
float OFFSCREEN_BUFFER = 500;
float STRIPE_SPACING = 50;
int numStripes = 0;
Stripe[] stripes;
void setup() {
float offset = 0;
size(800, 600, P2D);
smooth();
numStripes = (width + 2 * OFFSCREEN_BUFFER) / STRIPE_SPACING;
stripes = new Stripe[numStripes];
for (int i=0; i < numStripes; i++) {
stripes[i] = new Stripe();
stripes[i].x = offset;
offset = offset + inc;
}
Stripe.lastStripe = stripes[0];
}
void draw() {
background(255);
for (int i=0; i < numStripes; i++) {
stripes[i].draw();
stripes[i].move();
}
//blurAll();
}