0

选择汽车后,我需要在下一条语句中选择汽车

在此处输入图像描述

我删除了一些代码,只留下重要部分

Blockly.Blocks.car_selection = {
  init() {
    const cars = STORE.getCars();
    const carsSelection = cars.map(car => [car.name, car._id]);
    this.appendStatementInput('CarSelection')
      .appendField('Select the Car')
      .appendField(new Blockly.FieldDropdown(carsSelection), 'Car');
    this.setNextStatement(true, 'Number');
  },
};

Blockly.Blocks.car_movement = {
  init() {
    this.appendValueInput('Movement')
      .setCheck('Array')
      .appendField('Go X')
      .appendField(new Blockly.FieldNumber(0, 0, 20000), 'posX')
      .appendField('Y')
      .appendField(new Blockly.FieldNumber(0, 0, 20000), 'posY');
    this.setPreviousStatement(true, 'Number');
    this.setNextStatement(true, null);
  },
};

而且代码...

car_movementblockly中,我怎样才能选择汽车?

Blockly.JSON.car_selection = function(block) {
  const car = block.getFieldValue('Car');

  const statements = statementsCode
    .split('\n')
    .filter(it => it.length > 0)
    .map(it => JSON.parse(it));

  const command = {
    topic: `to/car/${robotIdentifier}`,
    packet: statements,
  };

  return `${JSON.stringify(command, null, 2).trim()}\n`;
};

Blockly.JSON.car_movement = function(block) {
  const x = block.getFieldValue('posX');
  const y = block.getFieldValue('posY');

  // TODO HOW TO GET CAR ID FROM PREVIOUS BLOCKLY ?
  // const car = STORE.getCar(carId);  <-- I NEED THE CAR ID

  const jsonSpec = {
    x,
    y,
    currentX: car.x,
    currentY: car.y,
  };

  return `${JSON.stringify(jsonSpec).trim()}\n`;
};
4

1 回答 1

-1

另一位 Blockly 用户在Blockly 论坛上发布了一个可能的解决方案:

https://groups.google.com/d/msg/blockly/btmwK-CwV6c/FUFoBJk-CAAJ

于 2017-07-20T15:04:10.283 回答