맞춤 블록 정의

블록은 프로그래밍하는 데 사용합니다. 텍스트 기반 프로그래밍 언어에서 표현식과 문을 나타냅니다.

블록 및 블록의 부분에 관한 자세한 내용은 시각적 용어집을 참고하세요.

블록 정의

블록 정의는 블록의 퍼즐 조각 연결과 필드를 지정합니다. 블록의 모양과 스타일은 대부분 다른 방법으로 지정됩니다. 블록이 변환되는 문자열 (일반적으로 코드)은 블록 코드 생성기로 정의됩니다.

간단한 블록을 정의하는 가장 쉬운 방법은 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);

앞으로 나아가기 위한 장애물

블록을 정의하고 툴박스에 추가하는 방법에 관한 자세한 내용은 맞춤 블록 개요를 참고하세요.