Avatar of Primoz Ivancic
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:
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
JavaScript* functions

Avatar of undefined
Last Comment
Primoz Ivancic

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Primoz Ivancic

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes