【问题标题】:Blockly while loop argument块状 while 循环参数
【发布时间】:2017-11-15 16:26:39
【问题描述】:

我用机器人创建了一个小迷宫,并使用 Blockly 生成代码来尝试解决它。我可以使用 Blockly 块的 Javascript 命令移动机器人。到现在为止还挺好。 我目前对 if 语句和 while 循环的争论感到头疼。主要是我尝试了两件事:

Blockly maze

  1. 创建一个变量“not_goal_reached”,它表示机器人是否已到达目标位置(十字)。代码:

function not_done() {
  var goal_location = get_goal_position()
  var goal_x = goal_location[0];
  var goal_y = goal_location[1];
  console.log('in not done');
  //console.log(player.x!= goal_x || player.y != goal_y)
  return (player.x!= goal_x || player.y != goal_y);
};

Blockly.Blocks['not_goal_reached'] = {
  init: function() {
    this.appendDummyInput()
        .appendField("not at goal")
    this.setOutput(true, "Boolean");
    this.setColour(230);
    this.setTooltip('');
    this.setHelpUrl('');
  }
};

Blockly.JavaScript['not_goal_reached'] = function(block) {
  
  var code = 'not_done()';
  // TODO: Change ORDER_NONE to the correct strength.
  //console.log(code)
  return [code, Blockly.JavaScript.ORDER_ATOMIC];
};

但是,当在 If 或 While 语句中使用此块时。我总是收到一个 Javascript 错误,无法帮助我找到解决方案:

TypeError: Cannot read property 'toBoolean' of undefined
    at Interpreter.stepConditionalExpression (acorn_interpreter.js:148)
    at Interpreter.step (acorn_interpreter.js:45)
    at nextStep (index.html:79)

我使用 Acorn js 解释器:

window.LoopTrap = 2000;
  //Blockly.JavaScript.INFINITE_LOOP_TRAP = 'if(--window.LoopTrap == 0) throw "Infinite loop.";\n';
	var code = Blockly.JavaScript.workspaceToCode(workspace);
	console.log(code);
	var myInterpreter = new Interpreter(code, initInterpreter);
	//Blockly.JavaScript.INFINITE_LOOP_TRAP = null
  

  var counter = 0;
  function nextStep() {
        try {
        if (myInterpreter.step()) {
          counter+=1;
          console.log(counter);
          if (counter < window.LoopTrap) {
              window.setTimeout(nextStep, 30);
              
            }
          else {
            throw "Infinite Loop!"
          }
        }
      }
  catch (e) {
        //alert(e);
        console.log(e)
    }
  } 
  
	nextStep();

问题:javascript错误我无法解决:(

  1. 我创建了自己的不需要输入的 While 块。这个 While 块在内部检查机器人是否已达到目标,然后处理 DO 语句:

Blockly.Blocks['repeat_forever'] = {
  init: function() {
    this.appendDummyInput()
        .appendField("While not at goal");
    this.appendStatementInput("DO")
        .appendField("Do");

    this.setPreviousStatement(true);
    this.setColour(230);
    this.setTooltip('');
    this.setHelpUrl('');
  }
};



Blockly.JavaScript['repeat_forever'] = function(block) {
  var branch = Blockly.JavaScript.statementToCode(block, 'DO');
  // TODO: Assemble JavaScript into code variable.
  //if (Blockly.JavaScript.INFINITE_LOOP_TRAP) {
  //  branch = Blockly.JavaScript.INFINITE_LOOP_TRAP.replace(/%1/g,
  //     '\'block_id_' + block.id + '\'') + branch;
  //  console.log(branch);
  //}

  var code = 'while (' + not_done() + ') {' + branch + '}';
  console.log(code)
  return [code, Blockly.JavaScript.ORDER_ATOMIC];

};

这可行,但是,这里我的问题是我的内部函数“not_done”只被评估一次(在代码生成时)到 while(true)(因为第一次机器人当然不在目标位置)。此块正确应用 DO 代码,但不会停止(因为 while (true))。如果我在“not_done()”周围添加引号,则该函数显然会被评估一次,但随后我收到与上面相同的 Javascript 错误(无法读取未定义的属性“toBoolean”)

我在这里遗漏了什么吗?非常感谢您的宝贵时间!

问候

K

【问题讨论】:

  • 你解决了吗?

标签: blockly


【解决方案1】:

您似乎设置了在 while 循环运行时无法达到的超时时间。

【讨论】:

    猜你喜欢
    • 2019-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-23
    • 1970-01-01
    • 2016-04-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多