2018年5月20日日曜日

Making custom blocks(with JavaScript) for micro:bit applications

This small article describes a feature of micro:bit visual block (with JavaScript). In particular, it is about user's block definition function. In the last article (No. 3), micro:bit was used as an indicator of pips of the dice. Dice is also commonly used in games and other applications.

Therefore it is convenient to have a custom block (user defined block) to display the dice 's eyes on the LED. To exceed the beginner level, it is essential to use this custom block function. A simple example is shown below. 


In the following, the function "show_pips_on_dice" was defined as a custom block. The argument denotes the number of pips of the dice. Every time you press button A, the eyes of the dice are displayed in ascending order. Thanks to the custom block, you can write it concisely like this.

Using custom block for displaying pips on a dice

The setting of this custom block is performed as shown below. Define this function "show_pips_on_dice (n: number)" in the namespace "custom". Then you can use (call) this function as a block from the menu "Custom". This definition content is stored in a file called "custom.ts".

Definition of a custom block for displaying pips on a dice


0 件のコメント:

コメントを投稿