gpt4 book ai didi

javascript - p5js ("syntaxerror : expected ; but found table"中的 xml 文件出错)

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

使用 p5.js,我尝试绘制 12 个具有随机颜色的正方形,按下鼠标时可以重新绘制这些正方形 - 我使用下面的代码进行了操作:

function setup() {
//build-up canvas w/ 12 squares
createCanvas(800, 600);

for (var y=0; y<600;y+=200) {
for (var x=0; x<800; x+=200) {
fill(random(255), random(255), random(255));
rect(x, y, 200, 200)
}
}
function touchStarted() {
// figure-out where to redraw square
var qX = (mouseX - (mouseX % 200)) / 200
var qY = (mouseY - (mouseY % 200)) / 200

fill(random(255), random(255), random(255));
rect(qX*200, qY*200, 200, 200);
}

但现在我正在尝试将数据保存在具有此类内容的 xml 文件中:

<build>
<square id="0" posx="0", posy="0">30</square>
<square id="1" posx="200", posy="0">60</square>

我正在尝试将其用作 ( https://p5js.org/reference/#/p5.XML ) 的引用:

var xml;

function preload() {
xml = loadXML("assets/data.xml");
}

function setup() {
// build-up canvas w/ 12 squares
createCanvas(800, 600);

var children = xml.getChildren("build");

for (var i=0; i < children.length; i++) {
var xpos = children[i].getNum("posx");
var ypos = children[i].getNum("posy");
var coul = children[i].getContent();
fill(coul);
rect(xpos, ypos, 200, 200);
}

但我只收到错误“SyntaxError:预期;但找到了表”,所以我真的迷路了......

感谢您的帮助!

最佳答案

试试这个:我已将 var Children = xml.getChildren("build"); 更改为 var Children = xml.getChildren("square");

var xml;

function preload() {
xml = loadXML("assets/data.xml");
}

function setup() {
// build-up canvas w/ 12 squares
createCanvas(800, 600);

var children = xml.getChildren("square"); // <----- CHANGED IT FROM BUILD TO SQUARE

for (var i=0; i < children.length; i++) {
var xpos = children[i].getNum("posx");
var ypos = children[i].getNum("posy");
var coul = children[i].getContent();
fill(coul);
rect(xpos, ypos, 200, 200);
}

另外,不要忘记关闭 xml 文件内部。应该看起来像这样:

<build>
<square id="0" posx="0", posy="0">30</square>
<square id="1" posx="200", posy="0">60</square>
</build>

关于javascript - p5js ("syntaxerror : expected ; but found table"中的 xml 文件出错),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47738115/

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