# Basic Array Question

**URL:** https://discourse.processing.org/t/basic-array-question/25077
**Category:** Coding Questions
**Created:** [October 31, 2020, 1:26pm UTC](https://discourse.processing.org/t/basic-array-question/25077 "2020-10-31T13:26:04Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![joe\_processing](https://avatars.discourse-cdn.com/v4/letter/j/aeb1de/32.png) [@joe\_processing](https://discourse.processing.org/u/joe_processing)
#### Post date: [October 31, 2020, 1:26pm UTC](https://discourse.processing.org/t/basic-array-question/25077/1 "2020-10-31T13:26:04Z")

</div>

Hi there!

I’ve noobie question…

I wrote a code that makes a ball appear when the key pressed minus or plus.

two questions:

1. I want every press will make one ball (now it looks like 3 at a time).
2. how do I make the array of negative to make a bigger ball and many balls (like 10 in one press).

```auto
Ball[] balls = new Ball[1];
float gravity = 0.1;

void setup() {
  size(800, 400);
  balls[0] = new Ball(255, 0, 0);
}

void draw() {
  background(#d75965);
  if ((keyPressed == true) && (key == '-')) {
    Ball b = new Ball(200, 100, 25); 
    balls = (Ball[]) append(balls, b);
  } else if ((keyPressed == true) && (key == '+')) {
    Ball b = new Ball(600, 100, 25); 
    balls = (Ball[]) append(balls, b);
  } else {
  }

  for (int i = 0; i < balls.length; i++ ) { 
    balls[i].gravity();
    balls[i].move();
    balls[i].display();
  }
  stroke (255);
  // strokeWeight(4);  
  line (400, 0, 400, 400);
}

class Ball {
  float x;
  float y;
  float speed;
  float w;
  
  Ball(float tempX, float tempY, float tempW) {
    x = tempX;
    y = tempY;
    w = tempW;
    speed = 0;
  }
  
  void gravity() {
    // Add gravity to speed
    speed = speed + gravity;
  }
  
  void move() {
    // Add speed to y location
    y = y + speed;
    // If square reaches the bottom
    // Reverse speed
    if (y > height) {
      speed = speed * -0.95;
      y = height;
    }
  }
  
  void display() {
    // Display the circle
    fill(#042060);
    stroke(100);
    ellipse(x,y,w,w);
  }
}  

```

thank you!

---

<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: [October 31, 2020, 6:26pm UTC](https://discourse.processing.org/t/basic-array-question/25077/2 "2020-10-31T18:26:45Z")

</div>

> [@joe\_processing](#):
>
> `keyPressed`

Instead of using this boolean use the function with the same name (see reference, it’s with …(), so `keyPressed()` )

It registers only once. Good.

To add 10 times use a for loop (int i=0; i\<10; i++)
