กำหนดบล็อกที่กำหนดเอง

บล็อกคือสิ่งที่คุณใช้เพื่อเขียนโปรแกรม ซึ่งแสดงนิพจน์และคำสั่งในภาษาโปรแกรมแบบข้อความ

ดูข้อมูลเพิ่มเติมเกี่ยวกับบล็อกและชิ้นส่วนของบล็อกได้ที่อภิธานศัพท์แบบภาพ

คําจํากัดความของบล็อก

นิยามของบล็อกจะระบุการเชื่อมต่อและช่องของชิ้นส่วนในบล็อก ลักษณะและสไตล์ส่วนใหญ่ของบล็อกจะระบุด้วยวิธีอื่นๆ สตริง (โดยปกติคือโค้ด) ที่บล็อกของคุณได้รับการแปลงจะเรียกว่าเครื่องมือสร้างโค้ดบล็อก

วิธีที่ง่ายที่สุดในการกําหนดบล็อกแบบง่ายคือการใช้ JSON

ข้อมูลโค้ดนี้กําหนดบล็อก "เลื่อนไปข้างหน้า" ที่มีการเชื่อมต่อถัดไปและก่อนหน้า และช่องสําหรับระยะทาง 1 ช่อง

// 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);

บล็อกสําหรับการเลื่อนไปข้างหน้า

ดูข้อมูลเพิ่มเติมเกี่ยวกับวิธีกำหนดบล็อกและเพิ่มลงในกล่องเครื่องมือได้ที่ภาพรวมของบล็อกที่กำหนดเอง