@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.