troubleshooting Question

Javascript object functions help

Avatar of Primoz Ivancic
Primoz Ivancic asked on
JavaScript* functions
1 Comment1 Solution117 ViewsLast Modified:
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);

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
Primoz Ivancic

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 1 Comment.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros