将API调用添加到JS解释器中,以与自定义阻止块一起使用

时间:2019-02-28 19:17:03

标签: javascript blockly google-blockly

在此之前,我想说一下我是JavaScript的新手,它是块状的,并且肯定可以使用Neil Fraser JS解释器。

我制作了一些自定义块,这些块只是创建JavaScript,当eval()将其块类型的对象和用户输入的内容放入数组中时。

他们用来执行此操作的函数称为pushInstruction(blockName,输入); 其中input是用户输入的块的数组,blockName是块的名称。

现在我正在尝试使用JS解释器,但是问题在于我如何将其与这些块一起使用。

我迫切需要帮助,并且一生中找不到任何资源可以帮助我。可能有些愚蠢。

自定义阻止代码

Blockly.Blocks['select_hand_position'] = {
  init: function() {
    this.appendDummyInput()
        .appendField("Move");
    this.appendDummyInput()
        .appendField(new Blockly.FieldDropdown([["left hand","Left hand"], ["right hand","Right hand"]]), "handSelect")
        .appendField("to index")
        .appendField(new Blockly.FieldNumber(0), "indexSelect");
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setColour(290);
 this.setTooltip("");
 this.setHelpUrl("");
  }
};
Blockly.JavaScript['select_hand_position'] = function(block) {
  var dropdown_handselect = block.getFieldValue('handSelect');
  var number_indexselect = block.getFieldValue('indexSelect'); 
  var input = '["'+dropdown_handselect+'",'+number_indexselect+']';
  var code = 'pushInstruction("select_hand_position",'+input+');'               
  return code;
};

我有一个全局数组来保存对象

instructionStructure = new Array();

然后在此函数中使用,其中是这些块生成要使用的代码的

function pushInstruction(blockName,inputs) {  
  var instruction = null;
  switch(blockName) {
    case "place_book":
    case "grab_book":
    case "select_hand_position":
      instruction = {
        blockName: blockName,
        hand: inputs[0],
        index: inputs[1].toString()
      };
      instructionStructure.push(instruction);
      break;

    default:
      throw 'attempted to push unknown instruction block';
  }       
} 

步骤代码

这是在按下步骤按钮时运行的代码

function stepCode() {
  Blockly.JavaScript.STATEMENT_PREFIX = 'highlightBlock(%1);\n';
  Blockly.JavaScript.addReservedWords('highlightBlock');
  var code = Blockly.JavaScript.workspaceToCode(workspace);
  var myInterpreter = new Interpreter(code, initApi);
  function nextStep() {
    if (myInterpreter.step()) {
      window.setTimeout(nextStep, 1000);
    }
  }
  nextStep();
  alert(instructionStructure);
}

initAPI函数

这是我不断获得

的地方
  

未捕获的TypeError:Interpreter.setProperty不是函数

在线

  

Interpreter.setProperty(scope,'pushInstruction',         interpreter.createNativeFunction(wrapper));

function initApi(interpreter, scope) {

  var wrapper = function(id) {
    id = id ? id.toString() : '';
    return interpreter.createPrimitive(highlightBlock(id));
  };
  interpreter.setProperty(scope, 'highlightBlock',
      interpreter.createNativeFunction(wrapper));


  wrapper = function(blockName, inputs) {
    return pushInstruction(blockName,inputs);
  };
  Interpreter.setProperty(scope, 'pushInstruction',
      interpreter.createNativeFunction(wrapper));  
}

感谢您花时间阅读此书,非常感谢!

1 个答案:

答案 0 :(得分:0)

您在这里有错字:

Interpreter.setProperty(scope, 'pushInstruction',
  interpreter.createNativeFunction(wrapper));  

=>

interpreter.setProperty(scope, 'pushInstruction',
  interpreter.createNativeFunction(wrapper));  

那是Uncaught TypeError的意思。 这是这里唯一的问题吗?因为我真的没有

  

但是问题在于我如何将它们与这些块一起使用。

相关问题