2017-12-10 22 views
0

p5.js를 사용하면 마우스를 눌렀을 때 다시 그릴 수있는 임의의 색상으로 12 개의 사각형을 그려야합니다. 울부 짖는 소리 :p5js의 xml 파일 오류 ("syntaxerror : expected; but found table")

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 : Expected; 하지만 발견 표는 "그래서 난 정말 도움을 ...

감사를 잃었어요

답변

1

을이보십시오! I'v을 var children = xml.getChildren("build"); 또한

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); 
    } 

var children = xml.getChildren("square");에 froget하지 않는 변경 다음과 같이 표시되어야합니다.

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