I have a student working on Lesson 27 in Unit 3 and is having the below issues with his game. Any insight or suggestions are very much appreciated!
Here is the link to his project:
[Game Lab - Code.org]
What its supposed to do:
The speaker should mute and unmute the music when clicked.
Also, the record when clicked should change the music being played unless its muted.
What its doing:
When the speaker is clicked, It will mute the music but when clicked again, it changes the music being played.
Also, the record is not working.
(“m” variable is for music changing and “s” variable is for muting the music.)
Whew - this was tough to figure out because both of these issues involve needing something to occur only one time but the draw loop runs 30ish times per second. So, my solutions (they are definitely not the only solutions) involved many counters and timers so that things only happen one time. I also found the nested if statements a little confusing so I used a few compound conditionals. This means using the &&. The && means BOTH conditions have to be true. For example: if(mousePressedOver(sound) && s == “play” ) means when a user presses the sound button AND the s variable is “play”.
Here is how I broke down the issues and tried to solve them. Here is my remix.
Toggle the mute button
- I did need to change the mute button to two sprites vs just changing animation. This is so the button could physically be moved and only clicked on one time and switched with the play button.
- I also put in a timer so that the user couldn’t click too quickly to unmute or play. Without the timer, the mute/unmute was flashing back and forth. Again - this is just how I solved it.
- The ranMusic is also assigned here which will be associated with the two music clips to randomly choose a song. You could also just assign one or the other songs but I randomized it.
Getting record to work - play random songs or stop music
- There is music that plays on running because s = play and ranMusic =1.
- Then if the user clicks on the music button, and as long as the mute is not on (s==“play”) then a random song is played BUT a function had to be run so that the music played outside the draw loop so a function is called to play the music. The function is called only one time because as the two conditions to call the function (ranMusic == 1 or 2 and s==“play” are only true 1 time through the loop because ranMusic is set equal to 3 after one call the function. Otherwise the function is repeatedly called and the song loops and sounds pretty awful.
I think this works for the most part but could probably use some tweaks to fit your student needs better.
Thank you so very much for this! I know this was not an easy task and I was completely stumped! My students were very excited to see one solution to fix their issues! Thanks again!