# Array syntax in this code does not hold y variable

**URL:** <https://discourse.processing.org/t/array-syntax-in-this-code-does-not-hold-y-variable/10637>\
**Category:** Coding Questions\
**Created:** [April 25, 2019, 5:38pm UTC](https://discourse.processing.org/t/array-syntax-in-this-code-does-not-hold-y-variable/10637 "2019-04-25T17:38:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [April 25, 2019, 5:38pm UTC](https://discourse.processing.org/t/array-syntax-in-this-code-does-not-hold-y-variable/10637/1 "2019-04-25T17:38:16Z")

</div>

Referring to Dan Shiffman book **_Learning Processing_ Ch 9 Arrays ~ex 9-12~ p.182-3**

The y variable – **random(height)** – in the **“Zoogies” constructor** in setup does not maintain the random y variable assigned. (in contrast, the randomly assigned **x variable IS maintained** ).

When i run the code, **the random y positions appear positioned randomly for a moment, then** all of the Zoog objects bounce to the top of the screen and stay in a line straight across.

What is happening? **The Zoog objects should remain in their initial random y position…**

I have tried hard coding in numbers instead of using “random”, but the same results each time.

* * *

Here is the code:

```auto
Zoog[] zoogies = new Zoog [100];

void setup(){
  size(400,400);
  for (int i = 0; i < zoogies.length; i++){
    zoogies [i] = new Zoog (random(width), random(height), 30, 30, 8);
  }
}
void draw(){
  background(255);
  for (int i = 0; i < zoogies.length; i++){
    zoogies[i].display();
    zoogies[i].jiggle();
  }
}
class Zoog {

  float x;
  float y;
  float w;
  float h;
  float eyeSize;

  Zoog (float tempX, float tempY, float tempW, float tempH, float tempEyeSize) {
    x = tempX;
    y = tempY;
    w = tempW;
    h = tempH;
    eyeSize = tempEyeSize;
  }
  void jiggle() {
    x = x + random(-1, 1);
    y = y + random(-2, 2);

    x = constrain (x, 0, width);
    y = constrain (y, 50, height);
  }
  void display() {
    for (float i = y - h/3; i < y + h/2; i += 10) {
      stroke(0);
      line(x-w/4, i, x+w/4, i);
    }
    ellipseMode(CENTER);
    rectMode(CENTER);

    //Zoog's body
    stroke(0);
    fill(175);
    rect(x, y, w/6, h);

    //Zoog's head
    stroke(0);
    fill(255);
    ellipse(x, y-h, w, h);

    //Zoog's eyes
    fill(0);
    ellipse(x-w/3, y-h, eyeSize, eyeSize*2);
    ellipse(x+w/3, y-h, eyeSize, eyeSize*2);
    
    //Zoog's legs
    stroke(0);
    line(x-w/12, y+h/2, x-w/4, y=h/2+10);
    line(x+w/12, y+h/2, x+w/4, y=h/2+10);
  }
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 25, 2019, 5:51pm UTC](https://discourse.processing.org/t/array-syntax-in-this-code-does-not-hold-y-variable/10637/2 "2019-04-25T17:51:01Z")

</div>

> [@debxyz](#):
>
> line(x-w/12, y+h/2, x-w/4, y=h/2+10);  
> line(x+w/12, y+h/2, x+w/4, y=h/2+10);

in the last lines of the class there is (twice) a `=` instead of a `-` which destroys `y`

**Remark**

That was hard to spot

- I had to comment out different parts of the code one after the other and suddenly the y worked so I knew that part was guilty

---

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [April 25, 2019, 6:29pm UTC](https://discourse.processing.org/t/array-syntax-in-this-code-does-not-hold-y-variable/10637/3 "2019-04-25T18:29:41Z")

</div>

You’re correct, that took care of the problem,  
Thank you so much @Chrisir!
