>> DAVID J. MALAN: Let's get started.
When green flag clicked, let's first initialize a variable called Muted to
false or, more simply, 0.
So on our data palette, we go ahead and click Make a Variable.
Call this variable Muted.
It can work for all sprites.
And we'll go ahead and click OK.
>> Now notice we have a new puzzle piece called Muted.
We can drag any number of these puzzle pieces now into my program, so as to
initialize that value.
>> Now let's do something forever.
Forever listen for the user hitting the space bar on their keyboard.
So to do that, we're going to need a condition.
We're going to then need to sense whether or not the user has pressed
the space bar.
>> And if so, we're going to check the value of our muted variable.
And if it equals 0, we're going to change it to 1 or true.
And if it equals 1, we're going to change it to 0.
In other words, we're going to toggle its value.
>> So to do that, we need another condition.
Let's drag that into place.
We need another Boolean expression, this time testing for equality.
>> What do we want to test for equality?
Well, the value of that variable.
So let's drag that into place.
If it equals 0, then we want to change the value of that
variable from 0 to 1.
>> Else we want to change the value of that variable from,
presumably, 1 to 0.
And that's it.
We now have a program that is constantly listening, forever, for the
user hitting the space bar.
And as soon as he or she does, it's going to change the value of that
variable from 1 to 0 or 0 to 1.
>> But we haven't yet brought the sea lion to life.
So let's create another script that also is going to start when the green
flag is clicked and is also forever going to be doing something, but this
time checking the value of the muted variable before doing that thing.
>> When green flag clicked, forever check if the value of the muted
variable equals 0.
Because if not--
in other words, if the sea lion is not muted-- we do want
him to play a sound.
And that sound will be that of a sea lion.
We'll give him a couple of seconds respite.
>> And now we have two scripts, both of which will run in parallel but are
somehow intercommunicating by way of that shared state, that variable.
Let's click our green flag.
>> [SEA LION BARKING]
>> SPEAKER 1: We hear the sea lion immediately, because, again, we've
initialized muted to 0, or false.
But now let me hit that space bar.
>> [SEA LION BARKING]
SPEAKER 1: Now, you might think that the program has stopped.
But it's not, because, again, we have these two forever loops that are still
running in parallel.
So in other words, if I hit that space bar again--
>> [SEA LION BARKING]
>> SPEAKER 1: Our sea lion is back.
>> [SEA LION BARKING]