google blockly Jsinterpreter和interpreter步骤代码

时间:2015-08-13 12:30:20

标签: javascript blockly

我是网站开发人员并尝试使用Google Blockly开发迷宫游戏。

我正在这里打击,当我试图运行它阻止其工作正常时,我有块,但问题是它没有突出显示当前正在执行哪个功能。

这是理解的代码,我有这个块代码:

Blockly.Blocks['move_forward'] = {
    init: function() {
    this.appendDummyInput()
    .appendField("move forward");
    this.setPreviousStatement(true);
    this.setNextStatement(true);
    this.setColour(290);
    this.setTooltip('');
    this.setHelpUrl('http://www.example.com/');
  }
};

Blockly.Blocks['turn_left'] = {
   init: function() {
   this.appendDummyInput()
   .appendField("turn")
   .appendField(new Blockly.FieldDropdown([["left", "l"], ["right", "r"]]), "NAME");
    this.setPreviousStatement(true);
    this.setNextStatement(true);
    this.setColour(290);
    this.setTooltip('');
    this.setHelpUrl('http://www.example.com/');
  }
};

Blockly.Blocks['turn_right'] = {
    init: function() {
    this.appendDummyInput()
    .appendField("turn")
    .appendField(new Blockly.FieldDropdown([["right", "r"], ["left", "l"]]), "NAME");
    this.setPreviousStatement(true);
    this.setNextStatement(true);
    this.setColour(290);
    this.setTooltip('');
    this.setHelpUrl('http://www.example.com/');
  }
};
and this is related to Blockly javascript

Blockly.JavaScript['move_forward'] = function(block) {
  // TODO: Assemble JavaScript into code variable.
  // var code = 'moveForward(); \n';
  return 'moveForward(\'block_id_' + block.id + '\');\n';
};

Blockly.JavaScript['turn_left'] = function(block) {
  var dropdown_name = block.getFieldValue('NAME');
  // TODO: Assemble JavaScript into code variable.
  // var code = 'turnLeft();\n';
  return 'turnLeft(\'block_id_' + block.id + '\');\n';
};

Blockly.JavaScript['turn_right'] = function(block) {
  var dropdown_name = block.getFieldValue('NAME');
  // TODO: Assemble JavaScript into code variable.
  // var code = 'turnRight();\n';
  return 'turnRight(\'block_id_' + block.id + '\');\n';
};

I have moveForward(), turnLeft(), turnRight() functions.

var myInterpreter = null;
    function interpret(){
     var code = Blockly.JavaScript.workspaceToCode(workspace);
     myInterpreter = new Interpreter(code, initApi);
     Blockly.JavaScript.STATEMENT_PREFIX = 'highlightBlock(%1);\n';
     Blockly.JavaScript.addReservedWords('highlightBlock');
     console.log(myInterpreter);
     myInterpreter.run();
    }

function initApi(interpreter, scope){
    var wrapper;
    wrapper = function(id) {
    moveForward(0);
    };
    interpreter.setProperty(scope, 'moveForward',
    interpreter.createNativeFunction(wrapper));
    wrapper = function(id) {
    turnLeft(1);
    };
    interpreter.setProperty(scope, 'turnLeft',
    interpreter.createNativeFunction(wrapper));
    wrapper = function(id) {
    turnRight(2);
    };
    interpreter.setProperty(scope, 'turnRight',
    interpreter.createNativeFunction(wrapper));
    }

如何编写解释器步骤代码,我还需要突出显示当前在Blockly中执行的任何函数。

请帮助解决这个问题。

2 个答案:

答案 0 :(得分:3)

尝试将此代码添加到init函数

  Add an API function for highlighting blocks.
  var wrapper = function(id) {
    id = id ? id.toString() : '';
    return interpreter.createPrimitive(highlightBlock(id));
  };
  interpreter.setProperty(scope, 'highlightBlock',
      interpreter.createNativeFunction(wrapper));

和init函数之外的这个函数

var highlightPause = false;
function highlightBlock(id) {
  workspace.highlightBlock(id);
  highlightPause = true;
}

它适合我,也希望你

答案 1 :(得分:1)

如果您使用的是JS Intrepeter,则需要创建一个包装函数。

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

然后在运行代码之前,添加此行。

Blockly.JavaScript.STATEMENT_PREFIX = 'highlightBlock(%1);\n';
var code = Blockly.JavaScript.workspaceToCode(workspace);
var JsInterpreter = new Interpreter(code, initApi);