Web Design IES
Web Design in Easy Steps, now in its 7th Edition, shows you how to make effective websites that work on any device.
Sean McManus, author of Scratch Programming in Easy Steps and Cool Scratch Projects in Easy Steps, explains the difference between global and local variables in Scratch
When you make a variable, Scratch asks you whether you want it to be for all sprites or just for one sprite. It's important to get the answer right, because it has a big impact on how your variables work.
In short, a variable for all sprites (a global variable) can be seen, changed and used by any sprite. A variable for one sprite belongs to that sprite alone. Other sprites can see its value, using the [x position of Sprite 1] Sensing block, but they can't change it.
These variables are usually called local variables in other programming languages. Two sprites can have local variables with the same name, but they're different variables, which can have different values.
Create the variable this year for ALL SPRITES.
Create the variables birth year and my age FOR THIS SPRITE ONLY
Create the variables birth year and my age FOR THIS SPRITE ONLY
You now have a variable called "this year", which all sprites can use to calculate their age. Each sprite has a variable called "my age" and "birth year". Although these variables have the same name, they're different variables - the boy and girl have their own birth year and ages, and other sprites can't change them.
This becomes really useful when you start duplicating sprites. If you duplicate a sprite that has a local variable, the new sprite will also have a local variable with that name. That stops sprites from interfering with each other's information. What happens if you use a global variable for "my age" or "birth year"? The sprites all start changing the same values, so their ages can get muddled up.
When I made this program, I made the boy sprite and then duplicated it for the girl sprite and then just changed the value of the birth year variable (and the name and picture). Because they were local variables, I was able to do that. If they had been made for all sprites, all the sprites would have the same value in the age variable at the end of the program.
In the picture below, I've shown all the variables for the sprites, together with the sprites. I've also duplicated the girl sprite and made it into Gobo, with its own age and birth year.
Find more 10 block Scratch demos here. For more information on my books and more bonus content, visit the Scratch Programming in Easy Steps and Cool Scratch Projects in Easy Steps homepages.
© Sean McManus. All rights reserved.
Visit www.sean.co.uk for free chapters from Sean's coding books (including Mission Python, Scratch Programming in Easy Steps and Coder Academy) and more!
Web Design in Easy Steps, now in its 7th Edition, shows you how to make effective websites that work on any device.
Power up your Microsoft Excel skills with this powerful pocket-sized book of tips that will save you time and help you learn more from your spreadsheets.
This book, now fully updated for Scratch 3, will take you from the basics of the Scratch language into the depths of its more advanced features. A great way to start programming.
Code a space adventure game in this Python programming book published by No Starch Press.
Discover how to make 3D games, create mazes, build a drum machine, make a game with cartoon animals and more!
In this entertaining techno-thriller for adults, Sean McManus takes a slice through the music industry: from the boardroom to the stage; from the studio to the record fair.