自訂構成元素
本文旨在協助想要在 Blockly 中建立新區塊的開發人員。我們假設只有一個可編輯 Blockly 的本機副本;其中一份副本熟悉 Blockly 的用法,且對 JavaScript 有基本的瞭解。

「模塊」提供許多預先定義的區塊。從數學函式到迴圈結構等等,全都沒問題。不過,為了與外部應用程式進行互動,您必須建立自訂區塊來建立 API。舉例來說,建立繪圖程式時,可能需要建立一個「半徑 R 的圓形」區塊。
在大多數情況下,最簡單的方法是找出非常類似的區塊,然後複製並視需求修改。下列說明文件適用於需要更多協助的使用者。
定義區塊
第一步是建立區塊,指定形狀、欄位和連接點。使用 Blockly 開發人員工具撰寫這類程式碼是最簡單的方法。
→ 進一步瞭解 Blockly 開發人員工具...
或者,也可以在研究 API 後手動編寫這個程式碼。
→ 進一步瞭解定義模塊...
進階區塊可能會根據使用者或其他因素,動態變更其形狀。
→ 進一步瞭解Mutators...
產生程式碼
第二步是建立區塊程式碼產生器,以將新區塊匯出至程式設計語言 (例如 JavaScript、Python、PHP、Lua 或 Dart)。
→ 進一步瞭解區塊程式碼產生器...
使用新的建塊
建立區塊後,別忘了將其新增至工具箱或在工作區中使用。
→ 進一步瞭解如何新增自訂構成元素...
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2024-06-28 (世界標準時間)。
[null,null,["上次更新時間:2024-06-28 (世界標準時間)。"],[[["This documentation guides developers on creating custom blocks within Blockly, assuming basic JavaScript knowledge and Blockly familiarity."],["Developers can create new blocks by defining their shape and connections using Blockly Developer Tools or by manually coding them using the API."],["Custom blocks require code generators to translate them into target programming languages like JavaScript or Python."],["After creation, custom blocks need to be integrated into the Blockly toolbox or workspace for usage."]]],["Developers creating custom blocks in Blockly should first define the block's shape, fields, and connections, ideally using Blockly Developer Tools. Next, a block-code generator is needed to translate the block into a programming language. Advanced blocks can use mutators to dynamically change shape. Finally, the new block must be added to the toolbox or used in a workspace for it to be accessible within the application. The document assumes the user has a local copy of Blockly and a basic Javascript knowledge.\n"]]