บล็อก

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

สำหรับข้อมูลเพิ่มเติมเกี่ยวกับบล็อกและรูปลักษณ์ของบล็อก โปรดดูอภิธานศัพท์ภาพ

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

คำจำกัดความของบล็อกจะระบุการเชื่อมต่อและช่องของจิ๊กซอว์ในบล็อก รูปลักษณ์และรูปแบบของบล็อกส่วนใหญ่ของคุณจะถูกระบุในลักษณะอื่น สตริง (โดยปกติจะเป็นโค้ด) ในการแปลงการบล็อกหมายถึงเครื่องมือสร้างโค้ดบล็อก

วิธีที่ง่ายที่สุดในการกำหนดบล็อกแบบง่ายคือการใช้ 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);

บล็อกสำหรับก้าวไปข้างหน้า

ดูข้อมูลเพิ่มเติมเกี่ยวกับวิธีกำหนดการบล็อกได้ที่กำหนดการบล็อก

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