Scratch exercise 61

Audio fader

In this exercise you will make a stop/start button to play a sound and an audio fader that controls the volume of the sound.

To make a fader work you must have a variable which records the vertical (y) position of the fader track on the stage (the fader can be moved wherever you want). This value can then be used to calculate the position of a fader cap (knob) as it is dragged up and down the fader track and convert this position data into volume data.

Another challenge is that the fader cap must 'stick" to the fader track and not be moved off it. Tricky!


1. Create a new Scratch game .. File>New


2. Delete the cat sprite.


3. Create these variables "for all sprites" ..


4. Paint your own stop/start button or download, save and import this sprite ...

Name the sprite "button" and also name each costume like in the image above.


5. Import a music loop onto the button sprite. In the button scripts below we have used a short one named "HipHop". You can download it here by right clicking.


6. Add these scripts to the button sprite..


7. Import this image and name it "fader track" ..

Ensure the costume centre point is in the middle of the vertical track ..

Note that this sprite is approximately 200 pixels high and this will be the travel of the fader cap (knob).


Add these scripts to it ..


8. Save your game.


9. Import this image and name it "fader cap" ..


10. Add these scripts to the fader cap sprite ..

Note again that in these scripts the fader track sprite is 200 pixels high and so the top position is defined by its position + 100 and its bottom by position - 100. Two of the if scripts prevent the fader from moving above or below these positions.


11. Position all the sprites on the stage. The fader cap start position is controlled by scripts. You don't need to see all the variables. Here's a suggestion ..

Note that we have changed the "current volume" variable on the stage to "large readout" and positioned it below the fader.


11. Save your game.


12. Press "B" to start the game and then click the stop/start button to start and stop the music loop playing. The fader cap will follow the mouse-pointer vertically and control the volume. Press "E" to end.


13. You're finished. Well done.