我想为我的blockly生成javascript,它没有任何输入。我从生成器存根获得了Javascript函数,但它需要将javascript代码汇编到 var code 变量中,而我并没有这样做。 我已经尝试过了:
var code = Blockly.JavaScript.workspaceToCode(workspace);
但它显示我的代码未定义。
我收到此错误:
Cannot read property 'call' of undefined at Blockly.Generator.blockToCode (blockly_compressed.js:1572) at Blockly.Generator.workspaceToCode (blockly_compressed.js:1570)
你能帮我解决一下我如何生成javascript代码并组装它?
答案 0 :(得分:2)
这是生成JS代码并运行它的方法:
<script>
var workspace = Blockly.inject('blocklyDiv',
{media: '../../media/',
toolbox: document.getElementById('toolbox')});
Blockly.Xml.domToWorkspace(document.getElementById('startBlocks'),
workspace);
// Generate JavaScript code and display it.
function showCode() {
Blockly.JavaScript.INFINITE_LOOP_TRAP = null;
var code = Blockly.JavaScript.workspaceToCode(workspace);
alert(code);
}
// Generate JavaScript code and run it.
function runCode() {
window.LoopTrap = 1000;
Blockly.JavaScript.INFINITE_LOOP_TRAP =
'if (--window.LoopTrap == 0) throw "Infinite loop.";\n';
var code = Blockly.JavaScript.workspaceToCode(workspace);
Blockly.JavaScript.INFINITE_LOOP_TRAP = null;
try {
eval(code);
} catch (e) {
alert(e);
}
}
</script>