fixing links in docs
This commit is contained in:
@@ -27,5 +27,5 @@ basic.clearScreen()
|
||||
|
||||
### See also
|
||||
|
||||
[set brightness](/reference/led/set-brightness), [unplot](/reference/led/unplot), [plot](/reference/led/plot), [Image](/reference/image/image), [clear](/reference/basic/clear-screen)
|
||||
[set brightness](/reference/led/set-brightness), [unplot](/reference/led/unplot), [plot](/reference/led/plot), [Image](/reference/images/image), [clear](/reference/basic/clear-screen)
|
||||
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
# Comment
|
||||
|
||||
A note in code.
|
||||
|
||||
### @parent blocks/statement
|
||||
|
||||
A comment is a line of code that contains text, usually an explanation or a note. All comments are ignored during script execution.
|
||||
|
||||
### Block
|
||||
|
||||
Right click on any block and add a comment
|
||||
|
||||
### ~hint
|
||||
|
||||
To find out how to insert comments using the Blocks editor, see [the Blocks editor](/blocks/editor).
|
||||
|
||||
### ~
|
||||
|
||||
### Sample code with comments
|
||||
|
||||

|
||||
|
||||
### Commenting out code
|
||||
|
||||
During the debugging process, you may want to comment out a section of your code so that it doesn't run.
|
||||
|
||||
To comment out a block of code:
|
||||
|
||||
1. Right click on any block of code that you want to comment out.
|
||||
|
||||
1. Select add comment
|
||||
|
||||
When you want to uncomment your code, right click the on the comment, and then click delete block.
|
||||
|
||||
### See also
|
||||
|
||||
[Block editor](/blocks/editor)
|
||||
|
||||
59
docs/reference/event-handler.md
Normal file
59
docs/reference/event-handler.md
Normal file
@@ -0,0 +1,59 @@
|
||||
# event handler
|
||||
|
||||
Event handlers - how they work.
|
||||
|
||||
An event handler is code that is associated with a particular event, such as "button A pressed". You create (or register) the association between an event and an event handler by calling a function named "on <event>". After registering an event handler with an event, then whenever that event occurs, the event handler code executes.
|
||||
|
||||
### Registering an event handler
|
||||
|
||||
Functions named "on <event>" create an association between an event and the event handler code. For example, the following code registers the event handler (the code between the `do` and `end` keywords) with the event of a press of button A:
|
||||
|
||||
```blocks
|
||||
input.onButtonPressed(Button.A, () => {
|
||||
basic.showString("hello", 150)
|
||||
})
|
||||
```
|
||||
|
||||
After this code executes, then whenever button A is pressed in the future, the string "hello" will be printed.
|
||||
|
||||
### Event handlers are active for the entire program execution
|
||||
|
||||
Once you have registered an event handler for an event, like above, that event handler is active for the rest of the program execution. If you want to stop the string "hello" from printing each time button A is pressed then you need to arrange for the following code to execute:
|
||||
|
||||
```blocks
|
||||
input.onButtonPressed(Button.A, () => {
|
||||
})
|
||||
```
|
||||
|
||||
The above code associated an event handler that does nothing with the event of a press of button A.
|
||||
|
||||
### There is only one event handler per event
|
||||
|
||||
The above example also illustrates that there is only one event handler for each event. What is the result of the following code?
|
||||
|
||||
```blocks
|
||||
input.onButtonPressed(Button.A, () => {
|
||||
basic.showString("hello", 150)
|
||||
})
|
||||
input.onButtonPressed(Button.A, () => {
|
||||
basic.showString("goodbye", 150)
|
||||
})
|
||||
```
|
||||
|
||||
The answer is that whenever button A is pressed, the string "goodbye" will be printed. If you want both the strings "hello" and "goodbye" to be printed, you need to write the code like this:
|
||||
|
||||
```blocks
|
||||
input.onButtonPressed(Button.A, () => {
|
||||
basic.showString("hello", 150)
|
||||
basic.showString("goodbye", 150)
|
||||
})
|
||||
```
|
||||
|
||||
### To learn more
|
||||
|
||||
To learn more about how the BBC micro:bit queues up and schedules event handlers, see [the BBC micro:bit - a reactive system](/device/reactive)
|
||||
|
||||
### see also
|
||||
|
||||
[on button pressed](/reference/input/on-button-pressed), [on logo up](/functions/on-logo-up), [on logo down](/functions/on-logo-down), [on screen up](/functions/on-screen-up), [on screen down](/functions/on-screen-down), [on shake](/reference/input/on-gesture), [on pin pressed](/reference/input/on-pin-pressed)
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
The clear function for images.
|
||||
|
||||
Turn off all the pixels in an [Image](/reference/image/image).
|
||||
Turn off all the pixels in an [Image](/reference/images/image).
|
||||
|
||||
### KindScript
|
||||
|
||||
@@ -35,5 +35,5 @@ input.onButtonPressed(Button.A, () => {
|
||||
|
||||
### See also
|
||||
|
||||
[Image](/reference/image/image), [show animation](/reference/basic/show-animation), [show image](/reference/images/show-image), [scroll image](/reference/images/scroll-image), [create image](/reference/images/create-image)
|
||||
[Image](/reference/images/image), [show animation](/reference/basic/show-animation), [show image](/reference/images/show-image), [scroll image](/reference/images/scroll-image), [create image](/reference/images/create-image)
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# Create Image
|
||||
|
||||
Create an [Image](/reference/image/image) to show on the [LED screen](/device/screen).
|
||||
Create an [Image](/reference/images/image) to show on the [LED screen](/device/screen).
|
||||
|
||||
```sig
|
||||
images.createImage(`
|
||||
@@ -35,5 +35,5 @@ input.onGesture(Gesture.Shake, () => {
|
||||
|
||||
### See also
|
||||
|
||||
[show animation](/reference/basic/show-animation), [image](/reference/image/image), [show image](/reference/image/show-image), [scroll image](/reference/image/scroll-image)
|
||||
[show animation](/reference/basic/show-animation), [image](/reference/images/image), [show image](/reference/image/show-image), [scroll image](/reference/image/scroll-image)
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
The pixel function.
|
||||
|
||||
Get the state of a pixel in an [Image](/reference/image/image).
|
||||
Get the state of a pixel in an [Image](/reference/images/image).
|
||||
|
||||
### KindScript
|
||||
|
||||
@@ -12,8 +12,8 @@ export function pixel(_this: micro_bit.Image, x: number, y: number) : boolean
|
||||
|
||||
### Parameters
|
||||
|
||||
* x - [Number](/reference/types/number); the *x coordinate* or horizontal position of a pixel in an [image](/reference/image/image)
|
||||
* y - [Number](/reference/types/number); the *y coordinate* or vertical position of a pixel in an [image](/reference/image/image)
|
||||
* x - [Number](/reference/types/number); the *x coordinate* or horizontal position of a pixel in an [image](/reference/images/image)
|
||||
* y - [Number](/reference/types/number); the *y coordinate* or vertical position of a pixel in an [image](/reference/images/image)
|
||||
|
||||
### x, y coordinates?
|
||||
|
||||
@@ -47,5 +47,5 @@ let state = img.pixel(0, 0)
|
||||
|
||||
### See also
|
||||
|
||||
[set pixel](/reference/images/set-pixel), [show image](/reference/images/show-image), [image](/reference/image/image), [create image](/reference/images/create-image), [scroll image](/reference/images/scroll-image)
|
||||
[set pixel](/reference/images/set-pixel), [show image](/reference/images/show-image), [image](/reference/images/image), [create image](/reference/images/create-image), [scroll image](/reference/images/scroll-image)
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
The plot frame function.
|
||||
|
||||
Display an [Image](/reference/image/image) on the BBC micro:bit's [LED screen](/device/screen)
|
||||
Display an [Image](/reference/images/image) on the BBC micro:bit's [LED screen](/device/screen)
|
||||
|
||||
### KindScript
|
||||
|
||||
@@ -37,5 +37,5 @@ img.plotFrame(1)
|
||||
|
||||
### See also
|
||||
|
||||
[create image](/reference/images/create-image), [show animation](/reference/basic/show-animation), [image](/reference/image/image), [show image](/reference/images/show-image), [scroll image](/reference/images/scroll-image)
|
||||
[create image](/reference/images/create-image), [show animation](/reference/basic/show-animation), [image](/reference/images/image), [show image](/reference/images/show-image), [scroll image](/reference/images/scroll-image)
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
The plot image function.
|
||||
|
||||
Display an [Image](/reference/image/image) on the BBC micro:bit's [LED screen](/device/screen)
|
||||
Display an [Image](/reference/images/image) on the BBC micro:bit's [LED screen](/device/screen)
|
||||
|
||||
### KindScript
|
||||
|
||||
@@ -37,5 +37,5 @@ img.plotImage(0)
|
||||
|
||||
### See also
|
||||
|
||||
[create image](/reference/images/create-image), [show animation](/reference/basic/show-animation), [image](/reference/image/image), [show image](/reference/images/show-image), [scroll image](/reference/images/scroll-image)
|
||||
[create image](/reference/images/create-image), [show animation](/reference/basic/show-animation), [image](/reference/images/image), [show image](/reference/images/show-image), [scroll image](/reference/images/scroll-image)
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
The scroll image function.
|
||||
|
||||
Scrolls the frames within an [Image](/reference/image/image) on the [LED screen](/device/screen).
|
||||
Scrolls the frames within an [Image](/reference/images/image) on the [LED screen](/device/screen).
|
||||
|
||||
### Block Editor
|
||||
|
||||
@@ -69,5 +69,5 @@ img.scrollImage(5, 1000)
|
||||
|
||||
### See also
|
||||
|
||||
[show image](/reference/images/show-image), [image](/reference/image/image), [create image](/reference/images/create-image), [show animation](/reference/basic/show-animation)
|
||||
[show image](/reference/images/show-image), [image](/reference/images/image), [create image](/reference/images/create-image), [show animation](/reference/basic/show-animation)
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
The set pixel function. #set pixel.
|
||||
|
||||
Set the on/off state of pixel in an [Image](/reference/image/image).
|
||||
Set the on/off state of pixel in an [Image](/reference/images/image).
|
||||
|
||||
### KindScript
|
||||
|
||||
@@ -12,8 +12,8 @@ export function setPixel(_this: micro_bit.Image, x: number, y: number, value: bo
|
||||
|
||||
### Parameters
|
||||
|
||||
* x - [Number](/reference/types/number); the *x coordinate* or horizontal position of a pixel in an [image](/reference/image/image)
|
||||
* x - [Number](/reference/types/number); the *y coordinate* or vertical position of a pixel in an [image](/reference/image/image)
|
||||
* x - [Number](/reference/types/number); the *x coordinate* or horizontal position of a pixel in an [image](/reference/images/image)
|
||||
* x - [Number](/reference/types/number); the *y coordinate* or vertical position of a pixel in an [image](/reference/images/image)
|
||||
* value -[Boolean](/reference/types/boolean); the on/off state of a pixel; `true` for on, `false` for off
|
||||
|
||||
### x, y coordinates?
|
||||
@@ -38,5 +38,5 @@ img.showImage(0)
|
||||
|
||||
### See also
|
||||
|
||||
[pixel](/reference/images/pixel), [show image](/reference/images/show-image), [image](/reference/image/image), [create image](/reference/images/create-image), [scroll image](/reference/images/scroll-image)
|
||||
[pixel](/reference/images/pixel), [show image](/reference/images/show-image), [image](/reference/images/image), [create image](/reference/images/create-image), [scroll image](/reference/images/scroll-image)
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
The show frame function.
|
||||
|
||||
Display an [Image](/reference/image/image) on the BBC micro:bit's [LED screen](/device/screen)
|
||||
Display an [Image](/reference/images/image) on the BBC micro:bit's [LED screen](/device/screen)
|
||||
|
||||
### KindScript
|
||||
|
||||
@@ -37,5 +37,5 @@ img.showFrame(1)
|
||||
|
||||
### See also
|
||||
|
||||
[create image](/reference/images/create-image), [show animation](/reference/basic/show-animation), [image](/reference/image/image), [show image](/reference/images/show-image), [scroll image](/reference/images/scroll-image)
|
||||
[create image](/reference/images/create-image), [show animation](/reference/basic/show-animation), [image](/reference/images/image), [show image](/reference/images/show-image), [scroll image](/reference/images/scroll-image)
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
The show image function.
|
||||
|
||||
Show an [Image](/reference/image/image) on the [LED screen](/device/screen), followed by a 400ms pause.
|
||||
Show an [Image](/reference/images/image) on the [LED screen](/device/screen), followed by a 400ms pause.
|
||||
|
||||
### Block Editor
|
||||
|
||||
@@ -20,7 +20,7 @@ export function showImage(_this: micro_bit.Image, xOffset: number)
|
||||
|
||||
### Create image and show image
|
||||
|
||||
Use the [image editor](/reference/image/image) to create images using the [create image](/reference/image/create-image) function, and then use `show image` like this:
|
||||
Use the [image editor](/reference/images/image) to create images using the [create image](/reference/image/create-image) function, and then use `show image` like this:
|
||||
|
||||
```
|
||||
let img = images.createImage(`
|
||||
@@ -57,5 +57,5 @@ for (let i = 0; i < 5; i++) {
|
||||
|
||||
### See also
|
||||
|
||||
[show animation](/reference/basic/show-animation), [image](/reference/image/image), [create image](/reference/images/create-image), [scroll image](/reference/images/scroll-image)
|
||||
[show animation](/reference/basic/show-animation), [image](/reference/images/image), [create image](/reference/images/create-image), [scroll image](/reference/images/scroll-image)
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
The width function.
|
||||
|
||||
Get the width of an [Image](/reference/image/image) in columns.
|
||||
Get the width of an [Image](/reference/images/image) in columns.
|
||||
|
||||
### KindScript
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# Plot LEDs
|
||||
|
||||
Display an [Image](/reference/image/image) on the BBC micro:bit's [LED screen](/device/screen). NOTE: `basic -> plot image` has been replaced by `basic -> show leds`.
|
||||
Display an [Image](/reference/images/image) on the BBC micro:bit's [LED screen](/device/screen). NOTE: `basic -> plot image` has been replaced by `basic -> show leds`.
|
||||
|
||||
### KindScript syntax
|
||||
|
||||
@@ -36,5 +36,5 @@ basic.plotLeds(`
|
||||
|
||||
### See also
|
||||
|
||||
[show animation](/reference/basic/show-animation), [image](/reference/image/image), [show image](/reference/images/show-image), [scroll image](/reference/images/scroll-image)
|
||||
[show animation](/reference/basic/show-animation), [image](/reference/images/image), [show image](/reference/images/show-image), [scroll image](/reference/images/scroll-image)
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# Screenshot
|
||||
|
||||
Make an [Image](/reference/image/image) out of the current state of the [LED screen](/device/screen).
|
||||
Make an [Image](/reference/images/image) out of the current state of the [LED screen](/device/screen).
|
||||
|
||||
```sig
|
||||
led.screenshot();
|
||||
@@ -12,7 +12,7 @@ led.screenshot();
|
||||
|
||||
### Returns
|
||||
|
||||
* an [Image](/reference/image/image) of what is currently visible on the [LED screen](/device/screen)
|
||||
* an [Image](/reference/images/image) of what is currently visible on the [LED screen](/device/screen)
|
||||
|
||||
### See also
|
||||
|
||||
|
||||
@@ -31,5 +31,5 @@ radio.onDataReceived(() => {
|
||||
|
||||
### See also
|
||||
|
||||
[receive number](/radio/receive-number), [send number](/radio/send-number), [set group](/reference/radio/set-group)
|
||||
[receive number](/reference/radio/receive-number), [send number](/radio/send-number), [set group](/reference/radio/set-group)
|
||||
|
||||
|
||||
@@ -30,5 +30,5 @@ input.onButtonPressed(Button.A, () => {
|
||||
|
||||
### See also
|
||||
|
||||
[receive number](/input/receive-number), [on data received](/reference/radio/on-data-received)
|
||||
[receive number](/reference/radio/receive-number), [on data received](/reference/radio/on-data-received)
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ radio.setGroup(1)
|
||||
|
||||
### Parameters
|
||||
|
||||
* ``id`` -- a [number](/number) between ``0`` and ``255``.
|
||||
* ``id`` -- a [number](/reference/types/number) between ``0`` and ``255``.
|
||||
|
||||
### Example
|
||||
|
||||
@@ -30,5 +30,5 @@ radio.setGroup(128)
|
||||
|
||||
### See also
|
||||
|
||||
[receive number](/radio/receive-number), [send number](/radio/send-number), [on data received](/radio/on-data-received)
|
||||
[receive number](/reference/radio/receive-number), [send number](/reference/radio/send-number), [on data received](/reference/radio/on-data-received)
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ How to define and use local variables.
|
||||
|
||||
### @parent language
|
||||
|
||||
A variable is a place where you can store and retrieve data. Variables have a name, a [type](/blocks/types), and value:
|
||||
A variable is a place where you can store and retrieve data. Variables have a name, a [type](/reference/types), and value:
|
||||
|
||||
* *name* is how you'll refer to the variable
|
||||
* *type* refers to the kind of data a variable can store
|
||||
@@ -12,12 +12,15 @@ A variable is a place where you can store and retrieve data. Variables have a na
|
||||
|
||||
### Var statement
|
||||
|
||||
Use the Block Editor variable statement to create a local variable and the [assignment operator](/reference/variables/assign) to store something in the variable.
|
||||
Use the Block Editor variable statement to create a variable
|
||||
and the [assignment operator](/reference/variables/assign)
|
||||
to store something in the variable.
|
||||
|
||||
For example, this code stores the number `2` in the `num1` variable:
|
||||
|
||||

|
||||
For example, this code stores the number `2` in the `x` variable:
|
||||
|
||||
```blocks
|
||||
let x = 2;
|
||||
```
|
||||
Here's how to define a variable in the Block Editor:
|
||||
|
||||
1. Click `variables`.
|
||||
@@ -26,57 +29,53 @@ Here's how to define a variable in the Block Editor:
|
||||
|
||||
3. Drag a block type on the right-side of the [assignment operator](/reference/variables/assign) and click the down arrow to change the variable name.
|
||||
|
||||
The resulting code should look something like this:
|
||||
|
||||
// string variable
|
||||
|
||||

|
||||
|
||||
// number variable
|
||||
|
||||

|
||||
|
||||
// boolean variable
|
||||
|
||||

|
||||
|
||||
// image variable
|
||||
|
||||

|
||||
|
||||
See [Image](/blocks/image) for info on creating and using image variables.
|
||||
|
||||
The resulting code should look something like this:
|
||||
|
||||

|
||||
|
||||
A variable is created for the number returned by the [brightness](/reference/led/brightness) function.
|
||||
|
||||
```blocks
|
||||
let b = led.brightness();
|
||||
```
|
||||
|
||||
### Using variables
|
||||
|
||||
Once you've defined a variable, just use the variable's name whenever you need what's stored in the variable. For example, the following code shows the value stored in `counter` on the LED screen:
|
||||
|
||||

|
||||
```blocks
|
||||
let counter = 1;
|
||||
basic.showNumber(counter);
|
||||
```
|
||||
|
||||
To change the contents of a variable use the assignment operator. The following code sets `counter` to 1 and then increments `counter` by 10:
|
||||
|
||||

|
||||
```blocks
|
||||
let counter = 1;
|
||||
counter = counter + 10;
|
||||
basic.showNumber(counter);
|
||||
```
|
||||
|
||||
### Why use variables?
|
||||
|
||||
Variables help simplify your code. For example, instead of turning on LEDs one by one like this:
|
||||
If you want to remember and modify data, you'll need a variable.
|
||||
A counter is a great example:
|
||||
|
||||

|
||||
|
||||
You can use a variable (`i`) and a [for loop](/reference/loops/for) to plot the same series of points (`i` is incremented by 1, each time the loop repeats):
|
||||
|
||||

|
||||
```blocks
|
||||
let counter = 0;
|
||||
input.onButtonPressed(Button.A, () => {
|
||||
counter = counter + 1;
|
||||
basic.showNumber(counter);
|
||||
});
|
||||
```
|
||||
|
||||
### Local variables
|
||||
|
||||
Local variables exist only within the function or block of code where they're defined. For example:
|
||||
|
||||

|
||||
```blocks
|
||||
// x does NOT exist here.
|
||||
if (led.brightness() > 128) {
|
||||
// x exists here
|
||||
let x = 0;
|
||||
}
|
||||
```
|
||||
|
||||
#### Notes
|
||||
|
||||
@@ -88,5 +87,5 @@ Local variables exist only within the function or block of code where they're de
|
||||
|
||||
### See also
|
||||
|
||||
[types](/blocks/types), [assignment operator](/reference/variables/assign)
|
||||
[types](/reference/types), [assignment operator](/reference/variables/assign)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user