In this screencast, you'll add "conditional" or "if/then" blocks to your program so your project plays a different sound depending on what color the user touches with the mouse.
For example, as the user moves the mouse, the yellow X sprite follows it. When the mouse stops on a green circle, an E note plays. When the X is touching both a green and brown circle, an E note and an A note play one after the other. Conditionals, or "if/then", blocks like this are one of the core computer science concepts that you'll see in all types of computer programs. They are what make a computer look like it is making decisions.
First, you need to tell the yellow X sprite to follow the mouse on the screen. You want the X sprite to follow the mouse, so make sure you click on the X sprite before you start adding code blocks.
The X should start following the mouse when the flag is clicked, so add a “When Flag Clicked” block from the Events menu.
Under the motion menu, drag out the “Go To Mouse-Pointer” block and add it under the “When Flag Clicked” block. Right now, the Yellow X Sprite is in the lower left of the stage. Click the Flag with the code you have now to see what happens. Great! But the X only moves once. Continuing to move the mouse doesn't change the yellow X. You want the yellow X to keep going to the mouse pointer over and over again forever once the green flag is clicked. To make that happen click on the Control menu, drag out a Forever block, and place it around the Go-To mouse pointer block. Now, when the green flag is clicked, the yellow X follows the mouse around forever and ever.
This isn’t much of a music project without any sounds! And, you want the notes played to be based on which color the mouse is touching. Use a "conditional" block - which is a block that says "If something is happening, then do something else." In this case, you'll want it to say "If the sprite is touching green, then play a certain musical note."
Click on the Control menu and drag out a "If blank then blank" block. Place it inside the forever loop because you want the condition to be checked the entire time the program is running. Right now, the If-then block says "If blank happens, then do blank" so running the block won't do anything at all. To fix that, go to the sensing menu, choose the 'touching color' block, and drag it into the if statement.
Snap it in so it says "If touching color, then... do blank" Change the color in the If block to one of the colors used on your stage by clicking on the small color block so your mouse cursor turns into a hand. Then, click on a color on the stage, like green, to update the touching color block to green. Next, go to the Sound menu and add a Play Note block to the If block. You can read this as a sentence now, "If the sprite is touching color green then play the note 60 for .05 beats."
See if that works by trying your code.
It's always important to test your project whenever you add new blocks so you can make sure the blocks are doing what you want them to do. It is easier to find a problem if you test often, when the changes you have made are small, rather than waiting until you add lots of blocks and trying them all out at once. Whenever the sprite is touching green, a note is playing, so it is working just as expected!
Of course, there are other colors in this project besides green , so you need to build an If statement like this for every color. To make this easy, right-click on the if-then block, choose duplicate, and put that block set right after the first one. That way, you only need to change the color and the note, so this saves a lot of time. Be sure to update the colors for each touching color block. You can use any notes or beat values you like in the play note block. You don't need to copy the values you see here. There is no one right answer...whatever results in a creation you like will be the right answer for you!
Now it's your turn! Make sure you have the yellow X sprite selected before you start coding. Add a “when flag clicked” block with a forever loop, and use a "go to mouse pointer" block to make your sprite follow the mouse. Then use a "if -then" block, a "touching color" block, and a "play sound" block to build code that says, "if the sprite is touching a certain color, play a certain sound." Then use the duplicate menu to repeat that if-then block set for each color in your project.
There are a lot of steps in this screencast! You’ll be fine, just take it one step at a time and remember to test your code blocks often and just keep trying new values until you have something you like. If you're stuck, put up your sticky or ask your neighbor.
- Make the X sprite go to the mouse.
- Make the X sprite follow the mouse FOREVER.
- Learn about the "if" block.
- Make the "if" block say "if touching color."
- Make the X sprite play a note if it is touching green.
- Copy the if statement, then change it to make each color in your project play a different note.
- Add a "when green flag is clicked" event to start the program.