can anyone tell me why this code doesn't work the way I expect it to?
function npc(name) {
this.name = name;
this.grid = [[0,9], [0,9]];
this.position = [0,0];
this.start = setInterval(function(){this.move() }, 1000);
this.stop = function(){clearInterval(this.start)};
this.move = function() {
this.position[0] = this.position[0] + Math.floor(Math.random() * 2);
this.position[1] = this.position[1] + Math.floor(Math.random() * 2);
if (this.position[0] > this.grid[0][1] || this.position[1] > this.grid[1[1]) {
this.position = [0,0];
};
console.log(this.name + " moved to " + this.position);
}
};
npc();
var bug = new npc("test-name");
bug.start();
bug.stop();
Bug.start() won't run unless I do npc() first, but even then it logs undefined as name and won't stop using the bug.stop()
Sorry if this is basic stuff, but I just can't work this out on my own...