Play a sound while bouncing
in
Core Library Questions
•
7 months ago
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!
- import ddf.minim.*;
- import ddf.minim.signals.*;
- import ddf.minim.analysis.*;
- import ddf.minim.effects.*;
- Minim minim;
- AudioPlayer fondo, sonidoRojo;
- float i;
- //creo las bolitas que rebotan
- Reboteiro rebotin01, rebotin02, rebotin03;
- void setup() {
- size (800, 600);
- background(0);
- smooth();
- i = 0;
- rebotin01 = new Reboteiro(450, 0, 0, 0, 0, 35, 0.8, height, 229, 49, 68);
- rebotin02 = new Reboteiro(430, -10, 0, 0, 0, 20, 0.85, height, 255, 255, 255);
- rebotin03 = new Reboteiro(470, -40, 0, 0, 0, 15, 0.82, height, 113, 226, 207);
- minim = new Minim(this);
- fondo = minim.loadFile("TUUUUMMMM_tunea.wav");
- sonidoRojo = minim.loadFile("tonico-formado-03v2.wav");
- }
- void draw() {
- background(0);
- fill(255,i+50);
- noStroke();
- ellipse(width/2, height/2-100, 150+i, 150+i);
- stroke(255, 100);
- noFill();
- ellipse(width/2, height/2-100, 160+i, 160+i);
- stroke(255, 100);
- noFill();
- strokeWeight(1);
- ellipse(width/2, height/2-100, 110+i, 110+i);
- i++;
- if (i > 120) {
- i =0;
- }
- if (fondo.isPlaying() == false){
- fondo.play(0);
- }
- rebotin01.dibujar();
- rebotin02.dibujar();
- rebotin03.dibujar();
- stroke(255, 100);
- line(-2, mouseY, 802, mouseY);
- rebotin01.piso=mouseY;
- rebotin02.piso=mouseY;
- rebotin03.piso=mouseY;
- //------> PLAYS THE SOUND <-------
- if (rebotin01.y > mouseY ){
- sonidoRojo.play(0);
- }
- }
- class Reboteiro {
- float x, y, speedY, speedX, gravity, movimientoX, gravit;
- int diametro, piso, r, g, b, rebotadas;
- Reboteiro(float x_, float y_, float speedX_, float speedY_, float movimientoX_, int diametro_, float gravit_, int piso_,
- int r_, int g_, int b_ ) {
- gravity = 0.5;
- x = x_;
- y = y_;
- speedX = speedX_;
- speedY = speedY_;
- movimientoX = movimientoX_;
- diametro = diametro_;
- gravit = gravit_;
- piso = piso_;
- r = r_;
- g = g_;
- b = b_;
- rebotadas = 0;
- }
- void dibujar() {
- fill(r, g, b);
- noStroke();
- ellipse(x, y, diametro, diametro);
- y = y + speedY; //change in position
- x = x + speedX; //change in position
- speedY = speedY + gravity; //acceleration
- if (y - diametro > piso) {
- y = piso;
- speedY = speedY * (-1*gravit); //damping upon rebound
- movimientoX = random(-0.05, 0.05);
- rebotadas++;
- if (rebotadas > 7){
- piso+=1500;
- }
- }
- speedX = speedX + movimientoX;
- if (x<0 || x>800) {
- speedX = -speedX;
- }
- }
- }
- void stop() {
- fondo.close();
- sonidoRojo.close();
- minim.stop();
- super.stop();
- }
1