Blockly while循环参数

时间:2017-11-15 16:26:40

标签: blockly

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

Blockly maze

  1. 创建一个变量'not_goal_reached',表示机器人是否已到达目标位置(交叉)。代码:
  2. 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语句:
    2. 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')

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

      问候

      ķ

1 个答案:

答案 0 :(得分:0)

似乎您设置了setTimeout,而while循环运行时无法达到。