The Counter Pattern

In Game Lab we relied on the counter pattern to continually update sprite properties and create animations. While the elements in App Lab don't have the same kinds of properties that sprites do, we can still apply the counter pattern to create some simple animations.

Like the draw loop in Game Lab, "data" events run a single block of code over and over again. Because these events run code repeatedly, you can use the counter pattern inside a "data" event to bring some simple movement to your program. You've been using the data event to track the accelerometer itself, but in order to make more complex programs you're going to have to update additional values outside of the accelerometer.

Moving Design Elements with the Counter Pattern

The above code uses the counter pattern in a "data" event to move a design element down the screen. In order to make our code more readable, we've saved the current "y" value position of "anchor" to a variable before using it in the counter pattern, but you could also write the pattern as:

setProperty("anchor", "y", getProperty("anchor", "y") + 1);

Found a bug in the documentation? Let us know at documentation@code.org