简体   繁体   中英

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. 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).

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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