Primoz Ivancic
asked on
Javascript object functions help
Hi all,
I'm new to javascript and I have no idea what I'm doing wrong. I have created a mazeObject fine, and would like to hold an array of mazeTile inside it, but when I try to define the objects in the array it creates them as a function rather than as the object.
mazeObject and instancing:
mazeTile (instancing in mazeObject.clear)
When maze.clear gets called and it creates the instance of all the mazetile objects, those tiles are of prototype function(), while the maze itself is something different.
And later when I try to use/read the tiles javascript crashes with no error (tried with just .contents or a function getcontents):
Any help would be appreciated as google has not been helpful
I'm new to javascript and I have no idea what I'm doing wrong. I have created a mazeObject fine, and would like to hold an array of mazeTile inside it, but when I try to define the objects in the array it creates them as a function rather than as the object.
mazeObject and instancing:
var mazeObject = function () {
var constructor = function mazeObject(width, height) {
var width;
var height;
var tiles;
this.width = width;
this.height = height;
this.resize = function (x, y) {
this.tiles = [];
// Creates all lines:
for (var i = 0; i < x; i++) {
// Creates an empty line
this.tiles.push([]);
// Adds cols to the empty line:
this.tiles[i].push(new Array(y));
}
this.clear()
}
this.clear = function () {
for (var i = 0; i < this.width; i++)
for (var j = 0; j < this.height; j++) {
this.tiles[i][j] = new mazeTile();
}
}
this.getTile = function (x, y) {
return this.tiles[x - 1, y - 1];
};
this.setTile = function (x, y, value) {
this.tiles[x - 1, y - 1].contents = value;
}
// Initialize the tiles
this.resize(width, height);
this.setTile(1, 1, tcDiamondDoor);
};
return constructor;
}();
var maze = new mazeObject(7, 5);
mazeTile (instancing in mazeObject.clear)
var mazeTile = function () {
var constructor = function mazeTile() {
var topWall = twNone;
var leftWall = twNone;
var otherWall = twoNone;
var contents = tcNone;
this.getContents = function () {
return contents;
}
}
return constructor;
}
When maze.clear gets called and it creates the instance of all the mazetile objects, those tiles are of prototype function(), while the maze itself is something different.
And later when I try to use/read the tiles javascript crashes with no error (tried with just .contents or a function getcontents):
if (tileRectContents[maze.getTile(i, j).contents] != tcNone)
{
drawImage(ctx, imgSpriteTile, tileRectContents[maze.getTile(i, j).getContents()], targetRect);
}
Any help would be appreciated as google has not been helpful
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.