So I made this game where you need to click on the ball that the eyes are following. and I when you click on the right ball i want more balls to appear. However, when i change numBalls to a higher number i get an error. When i change it to a smaller number there are no problems. No idea how to go about this.
- float bx = random (500);
- float by = random (500);
- float ballSpeedX = random (2.0,4.0);
- float ballSpeedY = random (2.0,4.0);
- int numBalls = 3;
- Ball[] balls = new Ball[numBalls];
- void setup ()
- {
- size (500,500);
- for (int i = 0; i < numBalls; i++) {
- balls[i] = new Ball(random(width), random(height), 20, i, balls, random (2.0,4.0), random (2.0,4.0));
- }
- }
- void draw ()
- {
- smooth ();
- background (0);
- face ();
- for (int i = 0; i<numBalls; i++) {
- balls[i].move();
- balls[i].display();
- println (i);
- }
- ball ();
- levels ();
- }
- void ball ()
- {
- bx += ballSpeedX;
- by += ballSpeedY;
- if (bx>width || bx<0) {ballSpeedX = ballSpeedX * -1;}
- if (by>height || by<0) {ballSpeedY = ballSpeedY * -1;}
- fill (color(#C61C1C));
- ellipseMode (CENTER);
- ellipse (bx,by,20,20);
- }
- void levels ()
- {
- if (mousePressed == true && mouseX > bx-10 && mouseX < bx+10 && mouseY > by-10 && mouseY < by+10)
- {delay (1000);
- numBalls = 2;
- setup ();
- }
- }
- void face ()
- {
- ellipseMode (CENTER);
- fill (color(#D6AFD1));
- ellipse (250-30,250,42,42);
- ellipse (250+30,250,42,42);
- fill (color(#FC08E0));
- ellipse ((bx/20)+206,(by/20)+238,10,10);
- ellipse ((bx/20)+266,(by/20)+238,10,10);
- }
- class Ball {
- float x,y;
- float diameter;
- float vx;
- float vy;
- int id;
- Ball [] others;
- Ball(float xin, float yin, float din, int idin, Ball[] oin, float vxin, float vyin) {
- x = xin;
- y = yin;
- diameter = din;
- id = idin;
- others = oin;
- vx = vxin;
- vy = vyin;
- }
- void move() {
- x += vx;
- y += vy;
- if (x + diameter/2 > width) {
- vx = vx*-1;
- }
- else if (x - diameter/2 < 0) {
- x = diameter/2;
- vx = vx*-1;
- }
- if (y + diameter/2 > height) {
- y = height - diameter/2;
- vy = vy*-1;
- }
- else if (y - diameter/2 < 0) {
- y = diameter/2;
- vy = vy*-1;
- }
- }
- void display() {
- fill(color(#C61C1C));
- ellipse(x, y, diameter, diameter);
- }
- }
1