Quantcast
Channel: Processing Forum
Viewing all articles
Browse latest Browse all 1768

Play a sound while bouncing

$
0
0
Hi!  I have this code and and want that when the red ball bounces upon the white line (that follows mouseY) plays a sound.  But what I got is that the sound is played one time after another inmediatellly.

Any help?

Thanks!

  1. import ddf.minim.*;
  2. import ddf.minim.signals.*;
  3. import ddf.minim.analysis.*;
  4. import ddf.minim.effects.*;

  5. Minim minim;
  6. AudioPlayer fondo, sonidoRojo;

  7. float i;

  8. //creo las bolitas que rebotan
  9. Reboteiro rebotin01, rebotin02, rebotin03;

  10. void setup() {
  11.   size (800, 600); 
  12.   background(0);
  13.   smooth();
  14.   i = 0;
  15.   
  16.   rebotin01 = new Reboteiro(450, 0, 0, 0, 0, 35, 0.8, height, 229, 49, 68);
  17.   rebotin02 = new Reboteiro(430, -10, 0, 0, 0, 20, 0.85, height, 255, 255, 255);
  18.   rebotin03 = new Reboteiro(470, -40, 0, 0, 0, 15, 0.82, height, 113, 226, 207);
  19.   
  20.   minim = new Minim(this);
  21.   fondo = minim.loadFile("TUUUUMMMM_tunea.wav");
  22.   sonidoRojo = minim.loadFile("tonico-formado-03v2.wav");

  23. }

  24. void draw() {
  25.     background(0);
  26.   
  27.     fill(255,i+50);
  28.     
  29.     noStroke();
  30.     ellipse(width/2, height/2-100, 150+i, 150+i);
  31.     
  32.     stroke(255, 100);
  33.     noFill();
  34.     ellipse(width/2, height/2-100, 160+i, 160+i);
  35.     
  36.     stroke(255, 100);
  37.     noFill();
  38.     strokeWeight(1);
  39.     ellipse(width/2, height/2-100, 110+i, 110+i);
  40.     i++;
  41.     
  42.     if (i > 120) {
  43.       i =0;
  44.     }
  45.   
  46.   if (fondo.isPlaying() == false){
  47.      fondo.play(0); 
  48.   }
  49.   
  50.   rebotin01.dibujar();
  51.   rebotin02.dibujar();
  52.   rebotin03.dibujar();
  53.   
  54.   stroke(255, 100);
  55.   line(-2, mouseY, 802, mouseY);
  56.   
  57.   rebotin01.piso=mouseY;
  58.   rebotin02.piso=mouseY;
  59.   rebotin03.piso=mouseY;
  60.   
  61. //------>  PLAYS THE SOUND <-------
  62.   if (rebotin01.y > mouseY ){
  63.       sonidoRojo.play(0); 
  64.   }

  65. }

  66. class Reboteiro {
  67.   float x, y, speedY, speedX, gravity, movimientoX, gravit;
  68.   int diametro, piso, r, g, b, rebotadas;

  69.   Reboteiro(float x_, float y_, float speedX_, float speedY_, float movimientoX_, int diametro_, float gravit_, int piso_,
  70.             int r_, int g_, int b_ ) {
  71.     gravity = 0.5;
  72.     x = x_;
  73.     y = y_;
  74.     speedX = speedX_;
  75.     speedY = speedY_;
  76.     movimientoX = movimientoX_;
  77.     diametro = diametro_;
  78.     gravit = gravit_;
  79.     piso = piso_;
  80.     r = r_;
  81.     g = g_;
  82.     b = b_;
  83.     rebotadas = 0;

  84.   }

  85.   void dibujar() {
  86.     fill(r, g, b);
  87.     noStroke();
  88.     ellipse(x, y, diametro, diametro);

  89.     y = y + speedY; //change in position
  90.     x = x + speedX; //change in position

  91.     speedY = speedY + gravity; //acceleration

  92.     if (y - diametro > piso) {
  93.       y = piso;
  94.       speedY = speedY * (-1*gravit); //damping upon rebound

  95.       movimientoX = random(-0.05, 0.05);
  96.       
  97.       rebotadas++;
  98.       
  99.       if (rebotadas > 7){
  100.          piso+=1500; 
  101.       }
  102.     }

  103.     speedX = speedX + movimientoX;

  104.     if (x<0 || x>800) {

  105.       speedX = -speedX;
  106.     }
  107.   }
  108. }


  109. void stop() {
  110.  
  111.   fondo.close();
  112.   sonidoRojo.close();

  113.   minim.stop();
  114.   super.stop();
  115. }

Viewing all articles
Browse latest Browse all 1768

Trending Articles