I'm getting used to JS still and I need a bit of help.
On the next class I want to call saveTiles inside loadMap since I want the lines array ( I can't return it, so I did the saveTiles() function ).
But when called I have a saveTiles is not defined error. I can't either use the prototype definition nor assign saveTiles to a var
What am I doing wrong? Is there a better thinking on how can I update the this.tiles property of Map ?
Thank you very much!
class Map{
constructor(){
this.xTiles = 32;
this.yTiles = 19;
this.tiles = new Array();
}
saveTiles(lines) {
var xMap = new Array();
var totalCont = 0;
while(totalCont < lines.length){
xMap.push(lines[totalCont++]);
if(totalCont % this.xTiles == this.xTiles - 1){
this.tiles.push(xMap);
xMap = [];
}
}
}
loadMap(map){
var rawFile = new XMLHttpRequest();
rawFile.open("GET", map, false);
rawFile.onreadystatechange = function (){
if(rawFile.readyState === 4)
if(rawFile.status === 200 || rawFile.status == 0){
var allText = rawFile.responseText;
var lines = allText.split(' ');
this.saveTiles(lines); // error -> saveTiles is not defined
}
};
rawFile.send(null);
}
}