The past two days we’ve discussed “Event” blocks and how they get our Sprites to act once we click, or trigger another Sprite to react. Today we’ll discuss variables and how they can trigger events.
July 9th – A Week With Scratch
If you are using Scratch 2.0 you’ll find variables under the “Data” section. Once you click on the section you’ll see two options, “Make a Variable” and “Make a List” – we’ll cover make a list later.
The variable feature is helpful and something that is within the natural progression of the program. I’ve seen after school clubs that show the event blocks, then let the eventual curiosity of Scratch take over. Within a few days (2-3) students either start asking or figure it out and start teaching each other. It can be a beautiful thing. Unfortunately in the upper grades (especially in High School when you’re only option for CS is often AP CS A) you are force fed variables very quickly.
Variables are similar to Math variables, and for the sake of this post, they are. It is a value that can change depending on the equation.
Two classic examples of variable usage are the timer and points.
Points need a simple “If” statement to trigger their value going up each time. In Scratch you can 1.) Create a variable (I called mine points) 2.) Set the value of the variable (I set mine at 0) 3.) and include in the “Forever loop” the “If” condition that if the Sprite is touching the other Sprite they get a point. Note: in my example I had the “Crab” randomly move around – but this could easily be an “avoid” game that subtracts points.
The timer is a little more complicated – only in a mistake that almost everyone makes right away. To create a timer: 1.) Create a variable (I called mine timer) 2.) Set the value of the variable (I set mine to 15 seconds) 3.) and include in the “Forever loop” the “Repeat” loop (with value set to 15) the 4.) Set a “wait 1 second” block for it to count down.
Using the blocks from yesterday, I have it set when the timer counts down from 5 to 0, then the Cat runs away. The blocks above are simple, the timer runs and “broadcast” a message (runAway) and then the Cat receives the message and does just that.