Play sound

edited June 2017 in Library Questions

Hey guys, how exactly do I play the sound file only when the ball hits the base (the rect)? Because all I'm doing is to make it play whenever it hits the whole bottom ..

Thank you

import ddf.minim.*;

Minim minim;
AudioPlayer certo;

void setup()
{
  size(1250,650);
 minim = new Minim(this);
 certo = minim.loadFile("FGBS(9).wav");
}

float bolaX = 200;
float bolaY = 100;
float velX = 10;
float velY = 0;
int hit = 0;
int miss = 0;


void draw()
{
  if(mousePressed) { hit = 0; miss = 0; }
  float base = 1000/ (hit+10);
  if(bolaX < 0 || bolaX > width) velX = -velX;
  if(bolaY > height){
    velY = -velY;
    float distancia = abs(mouseX - bolaX);
certo.play(); 
      certo.rewind();

    if (distancia < base) hit+=1;
    else miss += 1;
  } else velY += 1;



  bolaX += velX;
  bolaY += velY;

  noStroke();
  background(177, 195, 224);
  fill(252,252,252);
  ellipse(bolaX, bolaY, 50, 50);
  fill(33,33,33);
  rect(mouseX-base, height-15, 2*base, 15);

  textSize(20);
  fill(33,33,33);

  text("H I T: " + hit, 50, 50);
  text("M I S S: " + miss, 50, 100);


}
Tagged:

Answers

  • edited June 2017 Answer ✓

    Which lines play the sound? 29 and 30

    Maybe you need to swap them by the way

    The check for the base is in line 32/33

    I suggest you write this if statement with { } and move the two lines for the sound there into that if-clause

    Hints

    Hint: I tend to write all if / else blocks with { } around them

    Also hit ctrl-t regularly to auto format your code please

    Chrisir ;-)

Sign In or Register to comment.