Music loops too many times when game ends

[Posts in the debugging category need to have the following pieces of information included in order to help other teachers or our moderator team best help you. Feel free to delete everything in brackets before posting your request for debugging help.]

Link to the project or level: https://studio.code.org/projects/gamelab/G-24A-VeYjVkATt2ruTlpVNflk6jpfRetwoN1BM0mz8
What I expect to happen: [When game ends music changes to either losing or winning
What actually happens: [Music does play on winner or loser screen but both play over and over forever and then the game freezes and you have to refresh to stop sound
What I’ve tried: changing sound, moving, changing false/true options, adding a stop music
All other sounds are correct.

Didn’t want to leave you too long without an answer on this one, but I have been looking at it off and on the last 24 hours and still haven’t found a solution, but I will still keep looking at it. I am suspecting that because most of the sounds are called from the draw loop or from a function inside the draw loop that there is one or more sounds that are repeatedly executing and that is causing the issue. I may start removing one sound at a time to see if I can find the offending sound or sounds. What makes it even more confusing to me is that when I start the game, I sometimes get one sound and sometimes another and even refreshing the browser doesn’t always fix it, so this one is a bit tricky, but I’ll still look at it again when I get a minute and see if I can’t see something that is escaping me.

Mike

Thank you so much! :smiley: I greatly appreciate your help and trying.

My student has been working on it all week trying to debug too but she’s a “newbie” at coding and I’m not that much more experienced either.

Our school is on semester break starting tomorrow through Jan 6 so there’s no immediate rush.

THANKS AGAIN!

It looks like the problem is that the draw loop is running the winnerWinner or endGame function over and over, so the sound is playing over and over. Can you try doing something similar to how you saved the game state in order to create the start screen? For example, have a won variable that is originally set to false. Then, inside the winnerWinner function, instead of just checking to see whether (score >= 10), check for ((score >= 10) && (won ==false)). Then set won to true inside that code.

Elizabeth

2 Likes

Thanks for the reply. We just got back from winter break. I’ll get back with you after we tried it. Thank you so much for the suggestion!

1 Like

@elizabeth_admin A student in my class was having this same issue with the sound looping when she needed to finish out a winning and losing page. I tried this code suggestion for her to try to fix the issue and it did not solve the problem. I wanted to pass that along. I am sorry. However, there was some promise with the World.seconds code block. The nugget though is that it brought up an error message at the end even though it kind of worked to help improve the sound issues, it slowed down the sound and made it comprehensible and pleasant sounding at the very least. Can you please take a look at her Winning and Losing page code to give us some assistance here as we continue to try to solve this winning/losing page problem? Thanks. The project simulation of her project code is here in Gamelab. Thank you

Hi, @faithdwsn, The error message is caused because she is trying to assign World.seconds to a specific value and that isn’t allowed because World.seconds is defined already as the number of seconds that have passed since the game starts and since it’s native to the environment, it cannot be reassigned to a different value.

So I guess my question is, what is she trying to accomplish by setting World.seconds equal to 100? We would be happy to look at it again, but I just am not quite sure what that code is trying to do.

Best wishes!!!

Mike

@mwood

Hi Michael,

We are trying to create a winning and losing screen with specific finale sound clips assigned to each of them. The issue is that after the if statement enters the phase of those states it loops the sound into a frenetic distortion that plays forever and sounds terrible. We tried the code from Elizabeth to stop it and could not get it to stop the phenomenon.

The purpose of the World.seconds was that the error and the computer stopping the whole page, essentially stopped the processing and enabled the “winning” sound clip to play normally for 100 seconds and not distort, and vice versa for the losing page. Without the World.seconds block in there. The draw loop goes unbelievably fast and ruins both sound clips.

Same issue as the woman above, just reporting it from a different class. The only code block our class could find to slow down the final page and final sound clip was World.seconds – we know it is being used wrong hence the error message. We do not want to keep that error message, but the question is what do we replace it with that will still work? Nothing else that we try thus far has come even close to making sound comparable to that fix (error msg aside).

If you play the code it also helps to see in real-time. If you want I can remove the World.seconds block and then resend the link for you to experience the difference. Please let me know if you want me to do that? There is a big difference in the quality of the ending pages going back to being distorted.

Link to the project or level: [https://studio.code.org/projects/gamelab/ypRzTD_K7ui--GVJgZ5wwLqjQCIPksS5xnSHfP1yn24]
What I expect to happen: [When game ends music changes to either losing or winning page the final sound clip for that page will play clearly without distortion.]
What actually happens: [Music does play on winner or loser screen but both play over and over forever with DISTORTION and then the game freezes and you have to refresh to stop sound]
What I’ve tried:

Please note – all code remains the same as in the game lab link provided, other than the changes shown under the variables/functions in these trial sections.


TRIAL 1
Per suggestion in forum:

Try to make it set-up as a won variable that starts as false and then can be triggered to true to stop the cycle of sound loop.

var won = false; (Declared at the top)
function win() {
if ((score >= 10) && (won == false)) {
won == true;
stopSound(“sound://category_achievements/peaceful_win_2.mp3”);
playSound(“sound://category_achievements/peaceful_win_2.mp3”, true);
background(“lightblue”);
textSize(25);
text(“YOU WON”, 150, 200);
text(“congrats”, 160, 245);
t.visible = false;
shark.visible = false;
t.velocityX = 0;
t.velocityY = 0;
shark.velocityX = 0;
shark.velocityY = 0;
}
}

RESULTS | The game sound clip kept playing on distorted loop as usual at the end.


TRIAL 2

Try to change the play sound to stop sounds on both ends AND setting it to false
In addition to the setup in the earlier it statement.

var won = false; (Declared at the top)
function win() {
if ((score >= 10) && (won == false)) {
won == true;
stopSound(“sound://category_achievements/peaceful_win_2.mp3”);
playSound(“sound://category_achievements/peaceful_win_2.mp3”, false);
stopSound(“sound://category_achievements/peaceful_win_2.mp3”);
background(“lightblue”);
textSize(25);
text(“YOU WON”, 150, 200);
text(“congrats”, 160, 245);
t.visible = false;
shark.visible = false;
t.velocityX = 0;
t.velocityY = 0;
shark.velocityX = 0;
shark.velocityY = 0;
}
}

RESULT | The game sound clip kept playing on distorted loop as usual at the end.


TRIAL 3

Try to declare a stop variable and create a separate winStop and loseStop functions where the sound blocks are hopefully removed on the win/lose pages.

var stop = false; (Declared at top)

function draw() {

if (started) {
gameStart();

} else if (stop) {
winStop();
loseStop();

} else {

instructions();

if (keyDown(“enter”)) {

stopSound(“WaterWave3.mp3”);
stopSound(“sound://category_background/jazzy_beats.mp3”);
playSound(“WaterWave3.mp3”, true);
started = true;
}
}
}

function winStop() {
stop = true;
stopSound(“sound://category_achievements/peaceful_win_2.mp3”);
background(“lightblue”);
textSize(25);
text(“YOU WON”, 150, 200);
text(“congrats”, 160, 245);
t.visible = false;
shark.visible = false;
t.velocityX = 0;
t.velocityY = 0;
shark.velocityX = 0;
shark.velocityY = 0;
}

function loseStop {
stop = true;
stopSound(“sound://category_alerts/comedy_game_over_1.mp3”);
background(“black”);
textSize(25);
text(“YOU LOSE”, 150, 200);
t.visible = false;
fishes.visible = false;
t.velocityX = 0;
t.velocityY = 0;
shark.velocityX = 0;
shark.velocityY = 0;
}

function win() {
if (score >= 5) {
stopSound(“sound://category_achievements/peaceful_win_2.mp3”);
playSound(“sound://category_achievements/peaceful_win_2.mp3”, true);
background(“lightblue”);
textSize(25);
text(“YOU WON”, 150, 200);
text(“congrats”, 160, 245);
t.visible = false;
shark.visible = false;
t.velocityX = 0;
t.velocityY = 0;
shark.velocityX = 0;
shark.velocityY = 0;
stop = true;
}
}

function lose() {
if (lives <= 0) {
stopSound(“sound://category_alerts/comedy_game_over_1.mp3”);
playSound(“sound://category_alerts/comedy_game_over_1.mp3”, true);
background(“black”);
textSize(25);
text(“YOU LOSE”, 150, 200);
t.visible = false;
fishes.visible = false;
t.velocityX = 0;
t.velocityY = 0;
shark.velocityX = 0;
shark.velocityY = 0;
stop = true;
}
}

RESULTS | The goal was to try to bounce the final page to a function that does not contain a sound clip to bounce the sound clip to a STOP page. I am trying anything at this point, however, the game sound clip kept playing on distorted loop as usual at the end.


TRIAL 4

Use of World.seconds to slow down the computer at the end of the final frame by 100 seconds AND by using an error message. Thus slow the final sound clip to a normal speed.

In addition, use var soundPlay = true; to turn off the looping of the underwater sounds in draw loop the middle of the game. Also, carefully layer the middle stop sound blocks in sequence so that the sound clips do not run into each other.

Only relevant code below:

(Background sound at top of page)

playSound(“sound://category_background/jazzy_beats.mp3”, true);

var soundPlay = true;

function draw() {

if (started) {

gameStart();

} else {

instructions();

if (keyDown(“enter”)) {

stopSound(“WaterWave3.mp3”);

stopSound(“sound://category_background/jazzy_beats.mp3”);

playSound(“WaterWave3.mp3”, true);

started = true;
}
}
}

function win() {
if ((score >= 5) && (soundPlay)) {
stopSound(“sound://category_achievements/peaceful_win_2.mp3”);
playSound(“sound://category_achievements/peaceful_win_2.mp3”, true);
soundPlay=false;
background(“lightblue”);
textSize(25);
text(“YOU WON”, 150, 200);
text(“congrats”, 160, 245);
t.visible = false;
shark.visible = false;
t.velocityX = 0;
t.velocityY = 0;
shark.velocityX = 0;
shark.velocityY = 0;
World.seconds = 100;
}
}

function lose() {
if ((lives <= 0) && (soundPlay)) {
stopSound(“sound://category_alerts/comedy_game_over_1.mp3”);
playSound(“sound://category_alerts/comedy_game_over_1.mp3”, true);
soundPlay=false;
background(“black”);
textSize(25);
text(“YOU LOSE”, 150, 200);
t.visible = false;
fishes.visible = false;
t.velocityX = 0;
t.velocityY = 0;
shark.velocityX = 0;
shark.velocityY = 0;
World.seconds = 100;
}
}

RESULTS | The game sound clip sounded good on the winning and losing page. The images and background came up for the winning page as expected and stayed in place. The sound clip played pleasantly and at a speed where the player can make out the melody. It basically appears to work, EXCEPT, there is an error message – so it really does not work completely, except in theory. We still need to keep searching for a better answer.

Hi @faithdwsn,

So, I think I have a solution. My students and I were problem solving this issue before spring break. We knew the sound kept playing because although when the conditions should exist to stop the sound, it is almost as though it keeps checking to see if they exist each time through the loop so the sound keeps playing. I’m sure there are reasons for this including user error but we decided to work on a workaround. So, we asked what could we create that is only true for a fraction of a second. We created this super simple example. I tried it out in the WIN section of your code and I think it works. Right now, the stopSound stops after about 2 loops through the sound file but you can change that in the if statement.

Michelle

4 Likes

@melynn The work your class did over Spring break will pay off for many other classes because it was a God send for fixing this problem. You are being modest, because it was by no means super simple. It does not exist in the code.org documentation, it is a creative new way of using soundPlay = soundPlay+1; to create a sort of playSound seconds in an if statement to create a brand new solution of limiting time on sound loops.

This is the first time I have found this type of code on any forum, I have looked through all the other forums threads on stopping sound loops - and I am so thankful I found this! It is complex and requires a double layer of nested if statements. The way you demonstrated it in a simple way was helpful it will likely help other classes in the future.

Thank you Michelle.

Link to the project or level: Ocean Play Game
What actually happens: Now it works! :partying_face:

3 Likes

@faithdwsn -

So glad you found it helpful! I am guessing there are more solutions out there. That’s the fun with coding :slightly_smiling_face: ! Maybe this post can be a bank for solutions.

Thank again.
Michelle

Thank you!!! We’re having the same issue, (right before Spring Break :laughing:) two years later and your answer helped us so much! I have taught this course for a few years now and we have never put forth enough effort to solving this problem - my students usually just delete the sound. Today when I typed in “music looping code.org” this thread was the top response for me and your solution FINALLY worked for us! Thank you for sharing!

5 Likes