Using PShape in a for loop

I’m trying to use the PShape I created and make multiple ones within a for loop, but I’m having some trouble. I’d appreciate any help.

float variable;
PShape a;

void setup(){
  size(1680,1005,P3D);
  background(0);
}

void draw(){
  
  shape(a);
  
  for(float variable = 0; variable < 1005; variable = variable + 50);
  
  a = createShape();
  a.beginShape();
  a.fill(0, 0, 255);
  a.stroke(255);
  a.strokeWeight(0.25);
  a.vertex(250+variable, 500+variable);
  a.vertex(250+variable, 600+variable);
  a.vertex(350+variable, 600+variable);
  a.vertex(350+variable, 500+variable);
  a.endShape(CLOSE);
}
1 Like

this version does something

//float variable;
PShape a;

void setup() {
  size(1680, 1005, P3D);
  background(0);
}

void draw() {
  for (float variable = 0; variable < 1005; variable = variable + 50) {
    a = createShape();
    a.beginShape();
    a.fill(0, 0, 255);
    a.stroke(255);
    a.strokeWeight(0.25);
    a.vertex(250+variable, 500+variable);
    a.vertex(250+variable, 600+variable);
    a.vertex(350+variable, 600+variable);
    a.vertex(350+variable, 500+variable);
    a.endShape(CLOSE);
    shape(a);
  }
}


ok
-a- if you finish the for with ;

for();

it works, but not does repeat any later code lines
what you possibly expected?

-b- first must make a shape, then you can show it

-c- do you need the float var “variable” as global,
because as defined again local inside the for loop
does work well.

2 Likes

aw jeez, i left out the ‘shape(a)’… that’s kind of embarrassing lol.

i appreciate the help

also this

PShape a;

void setup() {
  size(1680, 1005, P3D);
  background(0);

  a = createShape();
  a.beginShape();
  a.fill(0, 0, 255);
  a.stroke(255);
  a.strokeWeight(0.25);
  a.vertex(250, 500);
  a.vertex(250, 600);
  a.vertex(350, 600);
  a.vertex(350, 500);
  a.endShape(CLOSE);
}

void draw() {
  for (float variable = 0; variable < 1005; variable = variable + 50) shape(a, variable, variable);
}


actually gives the same result, and would be a more efficient REUSE of a shape
https://processing.org/reference/shape_.html

if that is useful / what you can do with it / you need to find out
https://processing.org/reference/PShape.html
is very powerful, but must not fit to your concept.

test

PShape a;

void setup() {
  size(1680, 1005, P3D);
  background(0);

  a = createShape();
  a.beginShape();
  a.fill(0, 0, 255);
  a.stroke(255);
  a.strokeWeight(0.25);
  a.vertex(250, 500);
  a.vertex(250, 600);
  a.vertex(350, 600);
  a.vertex(350, 500);
  a.endShape(CLOSE);
  noLoop();
}

void draw() {
  for (float variable = 0; variable < 1005; variable = variable + 50) {
    a.setFill(color(random(255)));
    shape(a, variable, variable);
  }
}

2 Likes

how could I access individual shapes from the for loop with an array. Like, how could I index the for loop, so I can access individual Pshapes I created with that for loop?