Is it possible to use variables in ScratchJr?

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?

Happy coding!

6 comments

  1. Hello Weronika,
    thanks for this idea.
    Just one comment, I guess your first solution does not really constrain the variable in any way.
    I believe that if you really wanted to create “a numeric variable that can range between 0 and 6.”, you would have to combine both approaches, i.e. when add 2 conditionals
    – “when pointer bumps into number 7, move it back to 6”
    – “when pointer bumps into number -1, move it back to 0”
    Would you agree?
    Jiri

    1. Hi Jiri, thanks so much for your comment!
      Yes, you’re absolutely right – if you want your “variable” to be constrained to 0-6, you should add conditionals as you described (very good idea!)
      But I think that it really depends on your needs and the age of your students.For younger students, adding more “conditionals” might be just too complicated – a simple counter might be a good enough simulation for them. But more advanced students might enjoy understanding the constraints!

  2. The variable idea is great! how would you run it in parallel to the actual game you would be playing (as you cannot be controlling multiple pages together). Would this be built in small at the top of a game?

Leave a Reply

Your email address will not be published.