【问题标题】:google blockly Jsinterpreter and interpreter step codegoogle blockly Jsinterpreter 和解释器步骤代码
【发布时间】:2019-07-14 03:32:46
【问题描述】:

我是一名网络开发人员,正在尝试使用 Google Blockly 开发迷宫游戏。

我在这里感到震惊,当我尝试运行它时,我有块,它工作正常,但问题是它没有突出显示当前正在执行的函数。

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

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 中执行的任何函数。

请帮忙解决这个问题。

【问题讨论】:

    标签: javascript blockly


    【解决方案1】:

    尝试将此代码添加到您的 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;
    }
    

    它对我有用,希望对你也有用

    【讨论】:

      【解决方案2】:

      如果你使用的是 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);
      

      【讨论】:

        猜你喜欢
        • 2015-03-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-11
        • 1970-01-01
        • 1970-01-01
        • 2019-04-27
        • 2015-05-11
        相关资源
        最近更新 更多