Mission #04
Variables
“Can I use variables to
o
vari ables t control sounds and
to use s
Learn
h o w
e s ou r s o u nd
tim animations?”
h ow many ed.
cont ro l
s a re repeat
imation
and an
1. To save your scratch file to a par cular folder on the school
Objectives: 2.
network.
To create simple anima on using costumes.
Variables:
Variables store and count data (information). There are many
ways to use variables; we are going to use them to control the
#04
volume and how many times our sprites repeat their sound.
Call the variable
Repeat and select
Click on the Data
for this sprite only.
library and select
When you click ok more
Make a Variable.
block will appear.
Code:
We need to set the variable Repeat
to a value when our program is run.
From the Data library dray across
a set [repeat] to (0) block and
add it to our sequence.
The blocks is grey should already
be in your sequence.
Change the value to read
set [repeat] to (1).
Now we want to control how many times
our sprite repeats its sound.
From the control library drag across a
repeat (10) and add it to below the
when sprite clicked block.
We want to be able to change the amount
our repeat happens, so from the Data
library drag across a (Repeat) block and
insert it into where the (10) appears on the
repeat block
Code:
The variable will appear a the top of the screen.
Double click (click twice) and it will change to just a
number. Double click again and it will appear as a slider.
Move the variable underneath the sprite
Now lets make a volume control. On the Stage click the Data library
and select Make a Variable. Call it Volume.
Double click the volume variable
until it appears as a slider
then move it to the
middle of the screen.
Code:
Again we need to set a starting
value for our variable. From the
Data library drag across a
set [Volume] to (0) and change
the value to be (25).
This should be placed just under
the when clicked
From the sound library drag
across a set sound to (100) %
100 and add it above the play sound
in the forever loop.
We want the volume to change to
whatever the value of our variable
is. From the Data library drag
across a (volume) block and add it
into the set volume block.
Run your program and adjust the variable Volume
to see if our code is working.
Code:
We want our sprites to be louder than the background
music. To do this we can use that same variable.
From the sounds library drag
across a set volume to (100)%
and insert it below the
when this sprite clicked block.
From the Operators library drag
across a ( ) * ( ) block.
In computers * means times (x).
So this block will let us take a
value and times (x) it by another.
From the Data library drag across the
variable (Volume) and insert it into the
first space (field) then in the second
space (field) type the value 2.. This
will make our sounds twice as loud as
the background sounds.