Javascript object functions help

Primoz Ivancic
Primoz Ivancic used Ask the Experts™
on
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:
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);

Open in new window


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

Open in new window


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

Open in new window


Any help would be appreciated as google has not been helpful
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
turns out i was missing a () after defining my function object.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial