For starters, let’s be clear about it – there are no built-in blocks for conditionals and variables in ScratchJr. This, of course, is limiting, but on the other hand, the main advantage of Scratchjr is its simplicity. The program was designed for the youngest users, and not as a fully functional programming language.
Sometimes, however, you might want to be able to use a variable. For example, when you are creating a game and want to count the point scores of individual players.
In this post, I’ll show you how to create a simple “counter” to simulate a variable in ScratchJr.
Creating a counter to simulate a variable in ScratchJr
Let’ say you need a numeric variable that can range between 0 and 6. You need to see the current value of that variable and be able to increase or decrease the value as needed.
To simulate such functionality, draw a scale with values from 0 to 6.
Next, add a custom character – an arrow – that will point to the current value of the counter.
You can use messages to move the arrow to the right (to increase the value) and to the left (to decrease the value). Please check the animation below.
There is a counter and two buttons, one to increase and second one to decrease the current counter value.
Yes, this solution is very simplistic and has its obvious limitations, but it does the job!
Adding a “conditional statement”
Now, let’s say that we want to count to 6 points, and when a player reaches this result – we want to declare them a winner and stop the animation.
There is no explicit conditional statement block in ScratchJr, but we can use the “on bump” event to simulate conditionals… in the end, the logic is similar: “If any object bumps into given character, then do something…”
What we can do is put an additional character on our counter. When the arrow bumps into it, a desired action is triggered.
See the animation below. When the counter (the variable 😊) value is equal to 6, the game is over.
How do you like this idea? Do you find it useful?