Score counter Help

Hi, i have made a program to where balls would enter a basket and for every ball that enters (5 balls) + 10 to the counter and the balls would reset to the start.

I made my balls in an array:

Ball[] bballs = new Ball[5];

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

The problem is that when all the balls are sent into the basket at the same time, the counter would only update by +10 instead of +50.
If 1 ball was to enter by itself it would rarely update.
Any reason to why this is happening?

Without seeing the code that detects when a ball has entered the hoop, we are at a loss to help you. Ideally, you detect when a ball enters the hoop, and mark that ball as having entered - so you don’t count it again. You clear that it has been counted when it exits the hoop.

And you should have a counter that remembers how many balls have gone in. Once that counter reaches the amount required, score points are awarded and the counter’s value decreases.

Basket is on the bottom right of screen

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 = score + 10;

The balls bounce off a platform controlled by player and has to land in basket.

I do not understand how i can track/mark when a ball has entered, and how to implement a counter that remembers how many balls have gone in.

Instead of just awarding ten points for every ball, remember how many balls have gone in:

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 = score + 10;
      counter = counter + 1;
}

Now when enough have gone in, adjust the score and counter:

if( counter >= 5 ){
  counter = counter - 5;
  score = score + 50;
}

This then scores 50 points every five balls in, instead of 10 points for each ball.

I like this, I tried it in my code just like you did ( i had to make a new int counter; ).
Now the score is not updating when balls go in.

Also the balls do not go in sometimes all at the same time due to them each having different velocities and that is why i want to give each ball an individual score. There is gravity with the balls.

E.g. when all the balls enter, you can see the score gradually increase instead of waiting for all balls to go in and then adding 50.
Thank you for helping me :pray:

1 Like

I see why, the score doesn’t increase.
the score will increase by 10 once the balls have landed inside the basket 5.
The balls are not acting as if they are all separate:thinking:

1 Like