Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

How to initialize a variable in an array?

$
0
0

I'm trying to initialize my array so each ball in the array can be identified, yet I keep on getting errors. What I'm trying to do is, every time one of the balls bounce off the walls, it produces a sound.

    import ddf.minim.*;
    import ddf.minim.analysis.*;
    import ddf.minim.effects.*;
    import ddf.minim.signals.*;
    import ddf.minim.spi.*;
    import ddf.minim.ugens.*;
    import processing.sound.*;

    SoundFile tone;
    SoundFile tone2;
    SoundFile tone3;
    SoundFile tone4;
    SoundFile tone5;

    Bouncer[] bouncers = new Bouncer[4];

    //Position variables
      float x;
      float y;
    //Velocities
      float vx;
      float vy;
    //Size of the bouncers
      float size;

    void setup() {
      size(800,800);
       for (int i = 0; i < bouncers.length; i++) {
        // Each Bouncer just starts with random values
        bouncers[i] = new Bouncer(random(0,width),random(0,height),random(-10,10),random(-10,10),random(20,50));
     }
      // Load a sound by creating a new SoundFile and giving it the path to the file
      tone = new SoundFile(this, "tone01.wav");
      tone2 = new SoundFile(this, "tone02.wav");
      tone3 = new SoundFile(this, "tone03.wav");
      tone4 = new SoundFile(this, "tone04.wav");
      tone5 = new SoundFile(this, "tone05.wav");
    }
    void draw() {
        background(0);
       for (int i = 0; i < bouncers.length; i++) {
       bouncers[i].update();
       bouncers[i].display();

      }
    }


    class Bouncer {

    //Position variables
      float x;
      float y;


    //Velocities
      float vx;
      float vy;
    //Size of the bouncers
      float size;

      Bouncer(float tempX, float tempY, float tempVX, float tempVY, float tempSize) {
      x = tempX;
      y = tempY;
      vx = tempVX;
      vy = tempVY;
      size = tempSize;
      }

    //Adds the current location of the bouncer to its velocity, making it move
    //Checks if the bouncers are bouncing off the walls and acts accordingly
      void update() {
       x += vx;
       y += vy;
       bounce();
      }
    //Checks if the bouncers are bouncing off the walls
    //If the bouncers touch the edges, then it reverses its velocity
      void bounce() {
    //Checks if the bouncers are bouncing off the left and right border
       if (x-size/2 < 0 || x+size/2 >width) {
         vx = -vx;
       }
    //Checks if the bouncers are bouncing off the top and bottom borders
       if (y - size/2 < 0 || y + size/2 > height) {
         vy = -vy;
       }
      }
      void jingle() {

      }
    //Displays the bouncers
      void display() {
        noStroke();
        fill(0,0,255);
        ellipse(x,y,size,size);
      }
}

Viewing all articles
Browse latest Browse all 2896

Trending Articles