2016-03-26 00:47:20 +01:00
|
|
|
# counter activity
|
|
|
|
|
|
|
|
Display a number with a variable.
|
|
|
|
|
2017-09-07 22:42:08 +02:00
|
|
|
## ~avatar avatar
|
2016-03-26 00:47:20 +01:00
|
|
|
|
|
|
|
Welcome! This tutorial will teach you how to make a counter that increments when button A is pressed. Let's get started!
|
|
|
|
|
2017-09-07 22:42:08 +02:00
|
|
|
## ~
|
2016-03-26 00:47:20 +01:00
|
|
|
|
|
|
|
Let's start by creating a **local variable** `count` to keep track of the current count.
|
|
|
|
|
|
|
|
```blocks
|
|
|
|
let count = 0
|
|
|
|
```
|
|
|
|
|
|
|
|
The code under ``on button pressed("A")`` will run each time the user presses A. Let's add a line of code that increments `count` by `1`.
|
|
|
|
|
|
|
|
```blocks
|
|
|
|
let count = 0
|
|
|
|
input.onButtonPressed(Button.A, () => {
|
|
|
|
count = count + 1
|
|
|
|
})
|
|
|
|
```
|
|
|
|
|
|
|
|
Since the count has changed, it's time to refresh the screen display. Let's add a line of code to display the count on screen.
|
|
|
|
|
|
|
|
|
|
|
|
```blocks
|
|
|
|
let count = 0
|
|
|
|
input.onButtonPressed(Button.A, () => {
|
|
|
|
count = count + 1
|
|
|
|
basic.showNumber(count)
|
|
|
|
})
|
|
|
|
```
|
|
|
|
|
|
|
|
|
2017-09-07 22:42:08 +02:00
|
|
|
## ~avatar avatar
|
2016-03-26 00:47:20 +01:00
|
|
|
|
2016-04-13 17:27:45 +02:00
|
|
|
Excellent, you're ready to continue with the [challenges](/lessons/counter/challenges)
|
2016-03-26 00:47:20 +01:00
|
|
|
|
2017-09-07 22:42:08 +02:00
|
|
|
## ~
|
2016-03-26 00:47:20 +01:00
|
|
|
|