Cat says thank you

You give a treat. Cat gives you gratitude [maybe?].

Click on the Sketch Files drop down, you should see a “meow.wav” — open it and listen.

Let’s help the kitty say “thank you :3”

Load meow.wav

Create a variable to reference our sound, and load “meow.wav” into this variable in setup()

let meow;

function setup() {
  createCanvas(400, 400);
  meow = loadSound("meow.wav");
}

Play meow.wav

When would be a good time to play meow.wav?

How about when the treat disappears?

showTreat = false;
meow.play();

Try feeding the cat now. What happens?

Oh no it sounds like an alien space kitty! Cute but terrifying.

We need to make sure the sound plays once, and we can do that by changing the if statement's condition:

if(showTreat && dist(mouseX, mouseY, 200, 285) < 20) {
  // Hide treat, play sound
}

By requiring showTreat to be true, we ensure this code only runs once. Why? After we hide the treat, showTreat is false, which means it won’t run again.