简体   繁体   English

将自定义 Blockly 块嵌套到循环中并生成代码

[英]Nesting custom Blockly blocks into loops and generating the code

I am new to blockly and I am playing arround with creating custom blocks.我是块状的新手,我正在创建自定义块。

I've created a new file (move.js) in the blocks folder and there I created some custom blocks.我在 blocks 文件夹中创建了一个新文件 (move.js),并在那里创建了一些自定义块。 All of them have similar structure, like the one below它们都具有相似的结构,如下所示

Blockly.Blocks['move_forward'] = {
  init: function() {
    this.appendDummyInput()
        .appendField("Move Forward");
    this.appendDummyInput()
        .appendField(new Blockly.FieldImage("http://iosites.org/robotino/front.png", 20, 20, "Forward"));
    this.setInputsInline(true);
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setColour(120);
    this.setTooltip('');
    this.setHelpUrl('http://www.example.com/');
  }
};

Then I created a new file (move.js) in the generators/javascript folder and there I wrote very simple generators for the blocks (they only return a letter).然后我在 generators/javascript 文件夹中创建了一个新文件 (move.js),在那里我为块编写了非常简单的生成器(它们只返回一个字母)。

Blockly.JavaScript['move_forward'] = function(block) {
  return ['F;'];
};

The blocks work OK and return the text they are supposed to when stacked outside loops.这些块工作正常,并在堆叠在循环外时返回它们应该显示的文本。 But when I nest them inside a repeat or a while loop something happens and there is nothing returning.但是当我将它们嵌套在重复或 while 循环中时,会发生一些事情并且没有任何返回。 I have done some testing and I think that the problem occurs when the我做了一些测试,我认为问题发生在

Blockly.JavaScript.statementToCode

is called inside the repeat generator for my custom blocks.在我的自定义块的重复生成器内调用。

Hard to tell, but the generators usually returns either很难说,但生成器通常会返回

return code + '\n';

or或者

return [code, Blockly.JavaScript.ORDER_ATOMIC];

based on the block (if it returns something or just does something).基于块(如果它返回一些东西或只是做一些事情)。 You are generating (and returning) an array without the order (instead of just return 'F;'; )... not sure, but maybe that is causing the problem.您正在生成(并返回)一个没有顺序的数组(而不是只return 'F;'; )......不确定,但也许这导致了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM