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

how to play sound when mouse pressed

$
0
0

I want a 1 second sound to play when mouse is pressed in the following code that I have so far. I am new to programing and the problem is that I cannot find out where and how to write the code for adding the sound so that it plays simultaneously with the motion every time the mouse is pressed. I would greatly appreciate it if someone could tell me how to do this.

int num = 35;
float mx[] = new float[num];
float my[] = new float[num];

Mover[] movers = new Mover[10];

Liquid liquid;

void setup() {
  size(640, 660);
  reset();
  liquid = new Liquid(0, height/1.1, width, height/1.1, 0.1);
}

void draw() {
  background(0,111,98);

  int which = frameCount % num;
  mx[which] = mouseX;
  my[which] = mouseY;

  for (int i = 0; i < num; i++) {

    int index = (which+1 + i) % num;
    ellipse(mx[index], my[index], i, i);
  }


  liquid.display();

  for (Mover mover : movers) {

    if (liquid.contains(mover)) {
      PVector drag = liquid.drag(mover);
      mover.applyForce(drag);
    }

    PVector gravity = new PVector(0, -0.1*mover.mass);
    mover.applyForce(gravity);

    mover.update();
    mover.display();
    mover.checkEdges();
  }

  fill(255);
  text("click mouse to reset", 10, 30);
}

void mousePressed() {
  reset();
}

void reset() {
  for (int i = 0; i < movers.length; i++) {
    movers[i] = new Mover(random(0.5, 3), 40+i*70, height);
  }
}



class Mover {

  PVector position;
  PVector velocity;
  PVector acceleration;

  float mass;

  Mover(float m, float x, float y) {
    mass = m;
    position = new PVector(x, y);
    velocity = new PVector(0, 0);
    acceleration = new PVector(0, 0);
  }

  void applyForce(PVector force) {
    // Divide by mass
    PVector f = PVector.div(force, mass);
    acceleration.add(f);
  }

  void update() {

    velocity.add(acceleration);
    position.add(velocity);
    acceleration.mult(0);
  }

  void display() {
    stroke(255);
    strokeWeight(1);
    fill(255, 255, 255);
    ellipse(position.x, position.y, mass*10, mass*10);
  }

  void checkEdges() {
    if (position.y > height) {
      velocity.y *= -0.9;
      position.y = 0;
    }
  }
}


class Liquid {


  float x, y, w, h;
  float c;

  Liquid(float x_, float y_, float w_, float h_, float c_) {
    x = x_;
    y = y_;
    w = w_;
    h = h_;
    c = c_;
  }

  boolean contains(Mover m) {
    PVector l = m.position;
    if (l.x > x && l.x < x + w && l.y > y && l.y < y + h) {
      return true;
    } else {
      return false;
    }
  }

  PVector drag(Mover m) {
    float speed = m.velocity.mag();
    float dragMagnitude = c * speed * speed;

    PVector drag = m.velocity.copy();
    drag.mult(2);

    drag.setMag(dragMagnitude);
    return drag;
  }

  void display() {
    noStroke();
    fill(0,111,98);
    rect(x, y, w, h);
  }
}

Viewing all articles
Browse latest Browse all 2896

Trending Articles