# Using PShape in a for loop

**URL:** <https://discourse.processing.org/t/using-pshape-in-a-for-loop/13230>\
**Category:** Coding Questions\
**Created:** [August 7, 2019, 3:46am UTC](https://discourse.processing.org/t/using-pshape-in-a-for-loop/13230 "2019-08-07T03:46:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![thedurf18](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@thedurf18](https://discourse.processing.org/u/thedurf18)\
**Post date:** [August 7, 2019, 3:46am UTC](https://discourse.processing.org/t/using-pshape-in-a-for-loop/13230/1 "2019-08-07T03:46:53Z")

</div>

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.

```auto
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);
}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [August 7, 2019, 4:05am UTC](https://discourse.processing.org/t/using-pshape-in-a-for-loop/13230/2 "2019-08-07T04:05:50Z")

</div>

this version does something

```auto
//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.

---

<div class="post-metadata">

**Author:** ![thedurf18](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@thedurf18](https://discourse.processing.org/u/thedurf18)\
**Post date:** [August 7, 2019, 4:10am UTC](https://discourse.processing.org/t/using-pshape-in-a-for-loop/13230/3 "2019-08-07T04:10:22Z")

</div>

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

i appreciate the help

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [August 7, 2019, 4:18am UTC](https://discourse.processing.org/t/using-pshape-in-a-for-loop/13230/4 "2019-08-07T04:18:56Z")

</div>

also this

```auto
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](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](https://processing.org/reference/PShape.html)  
is very powerful, but must not fit to your concept.

test

```auto
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);
  }
}

```

---

<div class="post-metadata">

**Author:** ![thedurf18](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@thedurf18](https://discourse.processing.org/u/thedurf18)\
**Post date:** [August 7, 2019, 4:20am UTC](https://discourse.processing.org/t/using-pshape-in-a-for-loop/13230/5 "2019-08-07T04:20:39Z")

</div>

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?
