Using Flags in MakeCode Arcade

Kiki Prottsman
Kiki’s Corner
Published in
2 min readJun 20, 2022
Using a flag to check the last item touched

In my last story, I suggested the possibility of using a flag to prevent something from happening. Let’s talk about what a flag is and why you might want to use one.

What is a flag in computer science?

You won’t be coding very long before you run across the need for a flag. In computer science, a flag is a value that you set in your program to signal that something has (or hasn’t) happened. A binary flag is a signal that only indicates yes or no, while string or numeric flags can indicate extra states or choices.

When would I need a flag?

There are all sorts of situations that can be made easier with the use of a flag. For instance, you might only want to increase your player’s score the first time it overlaps a coin, or check to see if your player found a key before you let them through the door. Flags are super helpful in these situations when used along with conditional statements.

How do I use a flag?

The most simple form of a flag is a variable. Initialize your variable when the program starts, then when something happens, set the variable to a value that lets your program know the flag has been triggered. Later, you can check for the triggered value before running an action in your code.

In the program below, I initialize my variable to “cup”, but I only want something to happen once the ball has been touched and I’ve set my flag to “ball”. I could have just as easily used the values “true” and “false” or 1 and 0, but I decided on “ball” and “cup” to be more clear about which item was last touched.

Take a look at my code below and see if you can figure out why I wanted to use a flag before giving the player a point.

When you figure it out, “Respond” to leave your explanation in the comments below.

--

--

Kiki Prottsman
Kiki’s Corner

Kiki is an author, educator, and the Director of Education for Microsoft MakeCode