Blocks

ブロックはプログラミングに使用するものです。テキストベースのプログラミング言語の式やステートメントを表現します。

ブロックとその部分の詳細については、ビジュアル用語集をご覧ください。

ブロックの定義

ブロックの定義では、ブロック内のパズルピースのコネクションとフィールドを指定します。ブロックの外観とスタイルの大半は、別の方法で指定されます。ブロックが変換される文字列(通常はコード)は、ブロックコード ジェネレータとして定義されます。

単純なブロックを定義する最も簡単な方法は JSON を使用することです。

このコード スニペットでは、次と前の接続を含む「移動」ブロックと、距離に対応する 1 つのフィールドを定義しています。

// Create the definition.
const definitions = Blockly.createBlockDefinitionsFromJsonArray([
  {
    // The type is like the "class name" for your block. It is used to construct
    // new instances. E.g. in the toolbox.
    type: 'my_custom_block',
    // The message defines the basic text of your block, and where inputs or
    // fields will be inserted.
    message0: 'move forward %1',
    args0: [
      // Each arg is associated with a %# in the message.
      // This one gets substituted for %1.
      {
        // The type specifies the kind of input or field to be inserted.
        type: 'field_number',
        // The name allows you to reference the field and get its value.
        name: 'FIELD_NAME',
      }
    ],
    // Adds an untyped previous connection to the top of the block.
    previousStatement: null,
    // Adds an untyped next connection to the bottom of the block.
    nextStatement: null,
  }
]);

// Register the definition.
Blockly.defineBlocks(definitions);

前進するためのブロック

ブロックを定義する方法の詳細については、ブロックを定義するをご覧ください。

ツールボックスにブロックを含める方法については、ツールボックスの概要をご覧ください。