Unit 3 Lesson 16 Circle 10 - Sound effects when you use isTouching block

I’m teaching Computer Science Discoveries for the first time to 7th & 8th Graders. We are on Unit 3 Lesson 16 - Movement 10. Help! My students want the computer to play a sound when the frog collides with the mushroom. When we add this code to the condition “frog isTouching mushroom”, we get a real echoing noise as a sound effect because it repeatedly plays the sound even though we have the loop effect turned to “false”. I believe this is because the sound is replayed the whole time the frog crosses the mushroom as the mushroom is several pixels wide? How do you get just ONE sound to play when the frog collides with the mushroom?

Lisa,

Think about what you are asking the block to do - everytime it makes contact it plays. Is there anything the students could add in to make it play once (like a variable)? Or use a timer to have it play once every few seconds if the condition is still met?

Sorry I can’t share code - this is an open forum and students can see if this was a place to post possible answers…

Brad

Could a solution to this be placed in the exemplars for this level? I am having a similar question but struggling to find a solution given the tools available for this particular level.

Here’s a tip for you to find a solution so it only plays ONE sound inside the draw loop. Use the == comparison operator. We wanted the program to make a “happy” noise when the frog jumped over the mushroom. To do this, find a coordinate where the mushroom will for sure cross on the X -axis. Example mushroom.x == 101 and IF the mushroom.x == 101 have it play a sound and it will only play once because it only equals 101 for a very short time. This was a fix for us. However, be selective when choosing your x-axis intercept number. If your counter pattern is mushroom.x = mushroom.x + 4 PLEASE note that only every 4th number works.

I actually reread the hint on the forum and worked through lunch with the idea - finally figured out this solution. I added a variable that would go up one on the counterloop and if it is 1 then create a sound and add to the score. When the mushroom went back to the other side, I reset the value to 0 to enable the sound to play on the next pass of the mushroom.

// Create the frog

var frog = createSprite(175, 325);

frog.setAnimation(“frog”);

var score = 0;

var just_once = 0;

function draw() {

background(“skyblue”);

fill(“burlywood”);

noStroke();

rect(0, 360, 400, 40);

fill(“black”);

text(“score”, 0, 15);

text(score, 0, 50);

// if frog is on ground,

if (frog.y > 324) {

// if the user presses the up arrow

if(keyWentDown("up")){

  //make the frog move up

  frog.velocityY = -4;

  

} else {

  //make the frog stop moving

  frog.velocityY = 0;

  

}

}

// if the frog gets high enough

// send it back down

if (frog.y < 3) {

frog.velocityY = 4;

}

mushroom.velocityX = -3;

// if the mushroom has gone off the left edge,

if (mushroom.x < -30) {

// move it to the right edge

mushroom.x = 400;

just_once =0;

}

if (frog.isTouching(mushroom)) {

**just_once = just_once + 1;**

if (just_once == 1) {

score = score + 1;

playSound(“sound://category_hits/8bit_splat.mp3”, false);

}

}

drawSprites();

}

3 Likes