區塊就是您用來編寫程式的工具,這些類別代表文字程式設計語言的運算式和陳述式。
如要進一步瞭解區塊及其組成部分,請參閱視覺化詞彙表。
區塊定義
區塊定義可指定區塊上的謎題連線和欄位。大部分建塊的外觀和樣式都是以其他方式指定。區塊轉換為該字串 (通常是程式碼) 的字串會定義為區塊程式碼產生器。
定義簡單區塊最簡單的方法是使用 JSON。
這段程式碼定義了包含下一個和上一個連線的「向前移動」區塊,以及一個距離欄位。
// 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);
如要進一步瞭解如何定義區塊,請參閱「定義區塊」。
如要瞭解如何在工具箱中加入區塊,請參閱「工具箱總覽」。