gpt4 book ai didi

javascript - google blockly Jsinterpreter和解释器步骤代码

转载 作者:行者123 更新时间:2023-11-29 10:11:48 26 4
gpt4 key购买 nike

我是网络开发人员,正在尝试使用 Google Blockly 开发迷宫游戏。

我在这里很吃惊,当我尝试运行这个 block 时我有 block ,它工作正常,但问题是它没有突出显示当前正在执行的函数。

这里是理解代码,我有这个Blocks代码:

Blockly.Blocks['move_forward'] = {
init: function() {
this.appendDummyInput()
.appendField("move forward");
this.setPreviousStatement(true);
this.setNextStatement(true);
this.setColour(290);
this.setTooltip('');
this.setHelpUrl('http://www.example.com/');
}
};

Blockly.Blocks['turn_left'] = {
init: function() {
this.appendDummyInput()
.appendField("turn")
.appendField(new Blockly.FieldDropdown([["left", "l"], ["right", "r"]]), "NAME");
this.setPreviousStatement(true);
this.setNextStatement(true);
this.setColour(290);
this.setTooltip('');
this.setHelpUrl('http://www.example.com/');
}
};

Blockly.Blocks['turn_right'] = {
init: function() {
this.appendDummyInput()
.appendField("turn")
.appendField(new Blockly.FieldDropdown([["right", "r"], ["left", "l"]]), "NAME");
this.setPreviousStatement(true);
this.setNextStatement(true);
this.setColour(290);
this.setTooltip('');
this.setHelpUrl('http://www.example.com/');
}
};
and this is related to Blockly javascript

Blockly.JavaScript['move_forward'] = function(block) {
// TODO: Assemble JavaScript into code variable.
// var code = 'moveForward(); \n';
return 'moveForward(\'block_id_' + block.id + '\');\n';
};

Blockly.JavaScript['turn_left'] = function(block) {
var dropdown_name = block.getFieldValue('NAME');
// TODO: Assemble JavaScript into code variable.
// var code = 'turnLeft();\n';
return 'turnLeft(\'block_id_' + block.id + '\');\n';
};

Blockly.JavaScript['turn_right'] = function(block) {
var dropdown_name = block.getFieldValue('NAME');
// TODO: Assemble JavaScript into code variable.
// var code = 'turnRight();\n';
return 'turnRight(\'block_id_' + block.id + '\');\n';
};

I have moveForward(), turnLeft(), turnRight() functions.

var myInterpreter = null;
function interpret(){
var code = Blockly.JavaScript.workspaceToCode(workspace);
myInterpreter = new Interpreter(code, initApi);
Blockly.JavaScript.STATEMENT_PREFIX = 'highlightBlock(%1);\n';
Blockly.JavaScript.addReservedWords('highlightBlock');
console.log(myInterpreter);
myInterpreter.run();
}

function initApi(interpreter, scope){
var wrapper;
wrapper = function(id) {
moveForward(0);
};
interpreter.setProperty(scope, 'moveForward',
interpreter.createNativeFunction(wrapper));
wrapper = function(id) {
turnLeft(1);
};
interpreter.setProperty(scope, 'turnLeft',
interpreter.createNativeFunction(wrapper));
wrapper = function(id) {
turnRight(2);
};
interpreter.setProperty(scope, 'turnRight',
interpreter.createNativeFunction(wrapper));
}

如何编写解释器步骤代码,我需要突出显示函数以及当前在 Blockly 中执行的任何函数。

请帮忙解决这个问题。

最佳答案

尝试将此代码添加到您的初始化函数中

  Add an API function for highlighting blocks.
var wrapper = function(id) {
id = id ? id.toString() : '';
return interpreter.createPrimitive(highlightBlock(id));
};
interpreter.setProperty(scope, 'highlightBlock',
interpreter.createNativeFunction(wrapper));

这个函数在init函数之外

var highlightPause = false;
function highlightBlock(id) {
workspace.highlightBlock(id);
highlightPause = true;
}

对我有用,希望对你也有用

关于javascript - google blockly Jsinterpreter和解释器步骤代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31988318/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com