2020-01-17 13:26:50 +01:00
|
|
|
class SpriteSheetXML {
|
2020-03-21 17:22:28 +01:00
|
|
|
constructor(url) {
|
|
|
|
this.array = [];
|
|
|
|
this.fetchXMLtoArray(url);
|
|
|
|
}
|
2020-01-17 13:26:50 +01:00
|
|
|
|
2020-03-21 17:22:28 +01:00
|
|
|
fetchXMLtoArray(url) {
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
xhr.open("GET", url, false);
|
|
|
|
xhr.send(null);
|
2020-01-17 13:26:50 +01:00
|
|
|
|
2020-03-21 17:22:28 +01:00
|
|
|
if (xhr.status === 200) {
|
|
|
|
var children = xhr.responseXML.children[0].children;
|
|
|
|
for (let index = 0; index < children.length; index++) {
|
|
|
|
const element = children[index];
|
|
|
|
this.array.push({
|
|
|
|
name: element.attributes.name.nodeValue,
|
|
|
|
x: element.attributes.x.nodeValue,
|
|
|
|
y: element.attributes.y.nodeValue,
|
|
|
|
width: element.attributes.width.nodeValue,
|
|
|
|
height: element.attributes.height.nodeValue
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.error("XML file cannot be loaded!");
|
|
|
|
}
|
|
|
|
}
|
2020-01-17 13:26:50 +01:00
|
|
|
|
2020-03-21 17:22:28 +01:00
|
|
|
findIndex(attribute, value) {
|
|
|
|
for (let index = 0; index < this.array.length; index++) {
|
|
|
|
const element = this.array[index];
|
|
|
|
if (element[attribute] == value) {
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-17 13:26:50 +01:00
|
|
|
}
|
|
|
|
|
2020-02-29 13:45:56 +01:00
|
|
|
module.exports = SpriteSheetXML;
|