选择汽车后,我需要在下一个声明中选择汽车
我删除了一些只留下重要部分的代码
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_movement
块中,如何选择汽车?
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`;
};
答案 0 :(得分:0)
另一位Blockly用户在Blockly forum上发布了一个可能的解决方案:
https://groups.google.com/d/msg/blockly/btmwK-CwV6c/FUFoBJk-CAAJ