# Arrays causing problems?

**URL:** <https://discourse.processing.org/t/arrays-causing-problems/11595>\
**Category:** Coding Questions\
**Created:** [May 26, 2019, 2:59am UTC](https://discourse.processing.org/t/arrays-causing-problems/11595 "2019-05-26T02:59:19Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bobby](https://avatars.discourse-cdn.com/v4/letter/b/df788c/32.png) [@Bobby](https://discourse.processing.org/u/Bobby)\
**Post date:** [May 26, 2019, 2:59am UTC](https://discourse.processing.org/t/arrays-causing-problems/11595/1 "2019-05-26T02:59:19Z")

</div>

Hi, I am making a game where balls are being projected (with gravity) and the user has to make the balls bounce and land in a basket. If a ball drops, it should be game over.

There are so many problems with my code (this is just a part of it) and i am unsure what exactly is the cause of them. Ive created balls using arrays like this and i feel like this may be part of the problem:

```auto
Ball[] bballs = new Ball[5];

void setup(){
size(640,480);

// create balls
for(int i = 0; i<bballs.length; i++) {
bballs[i] = new Ball();
}
}

```

For example:

- When 1 ball falls on floor, game over screen shows WITH game still playing till the other balls drop
- When all balls fall, it works fine
- Score will only update with +1 when all balls go in basket instead of +5
- If 1 ball were to enter basket, ahead of the other balls, no change to score

Because i used an array, are the balls counted as 1 entity/object? Would really appreciate 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:** [May 26, 2019, 3:45am UTC](https://discourse.processing.org/t/arrays-causing-problems/11595/2 "2019-05-26T03:45:45Z")

</div>

no, the creation of your 5 bballs ( of type: class Ball ) looks good.  
so most probably the program logic causes the problems.

---

<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:** [May 26, 2019, 4:22am UTC](https://discourse.processing.org/t/arrays-causing-problems/11595/4 "2019-05-26T04:22:20Z")

</div>

check your logic:  
easy with a

```auto
println("score: "+score);

```

so you know that specific IF  
was working/executed  
and the score is counted up.

---

<div class="post-metadata">

**Author:** ![Bobby](https://avatars.discourse-cdn.com/v4/letter/b/df788c/32.png) [@Bobby](https://discourse.processing.org/u/Bobby)\
**Post date:** [May 26, 2019, 4:42am UTC](https://discourse.processing.org/t/arrays-causing-problems/11595/5 "2019-05-26T04:42:19Z")

</div>

Its just going up by 1 when all the balls enter the basket instead of going up by 1 for EACH ball that enters.  
The only part of my code that tracks the how many of the balls go in is the above code

---

<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:** [May 26, 2019, 4:46am UTC](https://discourse.processing.org/t/arrays-causing-problems/11595/6 "2019-05-26T04:46:37Z")

</div>

> [@Bobby](#):
>
> The only part of my code that tracks the how many of the balls go in is the above code

and is that a part of the class Ball code?  
and how is it updated from draw(){}  
and is the “score” a local variable of Ball class or a global variable?

---

<div class="post-metadata">

**Author:** ![Bobby](https://avatars.discourse-cdn.com/v4/letter/b/df788c/32.png) [@Bobby](https://discourse.processing.org/u/Bobby)\
**Post date:** [May 26, 2019, 4:50am UTC](https://discourse.processing.org/t/arrays-causing-problems/11595/7 "2019-05-26T04:50:43Z")

</div>

Yes that is part of class Ball  
Dont know what you mean about how it is updated (sorry beginner-ish)  
And it is a local variable of Ball class

---

<div class="post-metadata">

**Author:** ![Bobby](https://avatars.discourse-cdn.com/v4/letter/b/df788c/32.png) [@Bobby](https://discourse.processing.org/u/Bobby)\
**Post date:** [May 26, 2019, 4:53am UTC](https://discourse.processing.org/t/arrays-causing-problems/11595/8 "2019-05-26T04:53:40Z")

</div>

Oh i see what the issue, it had to be a global variable. Thank you very much

Now im left with the problem of having:

- When 1 ball falls on floor, game over screen shows WITH game still playing till the other balls drop
- When all balls fall, it works fine

---

<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:** [May 26, 2019, 4:59am UTC](https://discourse.processing.org/t/arrays-causing-problems/11595/9 "2019-05-26T04:59:08Z")

</div>

and where is that logic?

---

<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:** [May 26, 2019, 5:15am UTC](https://discourse.processing.org/t/arrays-causing-problems/11595/11 "2019-05-26T05:15:52Z")

</div>

better just set a global variable  
boolean game\_over

and set it true if ONE of the bballs  
hit the ground.  
for that can use in the class a

```auto
boolean gameOver() {
  boolean isover = false;
  if((pos.y >= 440)){
    vel.y=0;
    vel.x=0;
    isover = true;
  }
return isover;
}

```

the gameOverScreen part should anyway be part of draw() ( level thinking )  
and not part of class.

---

<div class="post-metadata">

**Author:** ![Bobby](https://avatars.discourse-cdn.com/v4/letter/b/df788c/32.png) [@Bobby](https://discourse.processing.org/u/Bobby)\
**Post date:** [May 26, 2019, 5:23am UTC](https://discourse.processing.org/t/arrays-causing-problems/11595/12 "2019-05-26T05:23:27Z")

</div>

how can i set it as a global variable if it takes values FROM the class i.e. pos.y, vel.x ,vel,y.

---

<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:** [May 26, 2019, 8:21am UTC](https://discourse.processing.org/t/arrays-causing-problems/11595/13 "2019-05-26T08:21:34Z")

</div>

sorry, too complicated,  
same like with score, a global level or screen variable

```auto
int many = 5;
Ball[] balls = new Ball[many];
int score=0, level=0;

void setup() {
  size(640, 480);
  for (int i = 0; i<many; i++) balls[i] = new Ball(); // create balls
}

void draw() {
  background(200, 200, 0);
  if ( level == 0 ) { // play balls
    for (int i = 0; i<many; i++) balls[i].draw();
  }
  if ( level == 1 ) { // game over
    background(#FF943B);
    fill(0, 0, 200);
    text("GAME OVER", width/2, height/2-40);
    text("Press Mouse To Retry", width/2, height/2);
    text("Your Score: " + "$" + score, width/2, height/2+80);
  }
}

class Ball {
  PVector pos, vel;

  Ball() {
    pos = new PVector( random(0, 26), random(0, 5));
    vel = new PVector(random(1.15, 1.2), random(0.2, 1.2));
  }

  void draw() {
    pos.add(vel);  
    circle(pos.x, pos.y, 10);

    if ((pos.y >380) && (pos.x >560)) { // resets ball if in basket
      pos.x = random(0, 26);
      pos.y = random(0, 5);
      vel.x = random(1.15, 1.2);
      vel.y = random(0.2, 1.2);
      score += 1;
      println("score "+score);
    }

    if ((pos.y >= 440)) level+= 1; // GAME OVER
  } // end draw
} // end class

```

---

<div class="post-metadata">

**Author:** ![Bobby](https://avatars.discourse-cdn.com/v4/letter/b/df788c/32.png) [@Bobby](https://discourse.processing.org/u/Bobby)\
**Post date:** [May 26, 2019, 11:03am UTC](https://discourse.processing.org/t/arrays-causing-problems/11595/14 "2019-05-26T11:03:55Z")

</div>

yea… thanks for the help but it tried it and it just complicated the rest of my code.  
I feel like maybe there might be another way that is simpler.  
I see what you were trying to do though.
