0

我想为我的 blockly 生成 javascript,它没有任何输入。我从生成器存根中获得了 Javascript 函数,但它需要将 javascript 代码组装成我没有得到的var 代码变量。我已经试过了:

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代码并组装它吗?

4

1 回答 1

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>
于 2017-10-05T07:30:31.753 回答