gpt4 book ai didi

javascript - blocky - 未定义的属性

转载 作者:行者123 更新时间:2023-12-03 02:38:57 26 4
gpt4 key购买 nike

我正在尝试在 blockly 中创建自定义 block ,但似乎无法让它工作。我从 block factory 生成代码这就是我得到的:

//say_input.js

Blockly.Blocks['say_input'] = {
init: function() {
this.appendDummyInput()
.appendField("say")
.appendField(new Blockly.FieldTextInput("something"), "say_input");
this.setColour(230);
this.setTooltip("");
this.setHelpUrl("");
}
};

Blockly.JavaScript['say_input'] = function(block) {
var text_say_input = block.getFieldValue('say_input');
// TODO: Assemble JavaScript into code variable.
// var code = 'alert("Hello! I am an alert box and'+text_say_input+'!");';
var code = '...;\n';
return code;
};

然后我将其导入到我的 html 中:

<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<title>Blockly Demo: Fixed Blockly</title>
<script src="js/blockly_compressed.js"></script>
<script src="js/blocks_compressed.js"></script>
<script src="js/msg/js/en.js"></script>

<script src="js/blocks/say_input.js"></script>
<style>
body {
background-color: #fff;
font-family: sans-serif;
}

h1 {
font-weight: normal;
font-size: 140%;
}
</style>
</head>

<body>
<button onclick="runCode()">Click me</button>
<div id="blocklyDiv" style="height: 480px; width: 600px;"></div>

<xml id="toolbox" style="display: none;">
<block type="say_input">
<field name="say_input">something</field>
</block>
</xml>

<script>
var demoWorkspace = Blockly.inject('blocklyDiv', {
media: 'js/media/',
toolbox: document.getElementById('toolbox')
});

function runCode(){
window.LoopTrap = 1000;
Blockly.JavaScript.INFINITE_LOOP_TRAP =
'if(--window.LoopTrap == 0) throw "Infinite loop."\n';
var code = Blockly.JavaScript.workspaceToCode(workspace);
Blockly.JavaScript.INFINITE_LOOP_TRAP = null;
try {
eval(code);
} catch (e) {
alert(e);
}
}
</script>

</body>

</html>

并将其加载到浏览器。我立即收到此错误:

Cannot set property 'say_input' of undefined

错误所在行:

Blockly.JavaScript['say_input'] = function(block) {

我的自定义 block 出现在工作场所中,因此我确信链接正常工作。

我检查了this video似乎我没有做错什么。

我该如何解决这个问题?

最佳答案

我找到了解决方案。我必须链接 javascript_compressed.js 然后一切正常。

<script src="js/javascript_compressed.js"></script>

确保将其链接到自定义 block.js 之前。

关于javascript - blocky - 未定义的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48413817/

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