定义自定义组成要素

您可以使用块进行编程。它们表示基于文本的编程语言中的表达式和语句。

如需详细了解代码块及其各个部分,请参阅可视化术语表

屏蔽设置

块定义用于指定块上的拼图块连接和字段。您可以通过其他方式指定大部分块的外观和样式。代码块转换成的字符串(通常是代码)被定义为代码块代码生成器

若要定义简单的块,最简单的方法是使用 JSON。

以下代码段定义了一个包含下一个和上一个连接的“向前移动”块,以及一个用于距离的字段。

// Create the definition.
const definitions = Blockly.common.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.common.defineBlocks(definitions);

用于前进的屏障

如需详细了解如何定义块并将其添加到工具箱,请参阅自定义块概览