Jump to content

javascript object creation and "n is undefined"


Recommended Posts


I'm tryng to create an object wich holds mine ground in a scene, I'm using this code but it doesn't works, in console I receive a "n is undefined" message.
But with the vector creation, for example, there's no problems....


var terreno = {	    noiseTexture : "media/noise2.jpg",    distortAmount : 10,    offset : -20,	    xmin : -200,    zmin : -200,    xmax :  200,    zmax :  200,    precision : {        "w" : 80,        "h" : 80    },    subdivisions : {        'h' : 1,        'w' : 1    },    // Create the Tiled Ground    create : new function(name, scene) { //"Tiled Ground"    	this.scene = scene;    	this.name = name;    	    	this.v = new BABYLON.Vector3(1, 10, 0);    	this.tiledGround = new BABYLON.Mesh.CreateTiledGround(name, this.xmin, this.zmin, this.xmax, this.zmax, this.subdivisions, this.precision, scene);    	    }        };
Link to comment
Share on other sites


I guess "new function(name, scene)" creates a new JS object, so the keyword this used inside this function could not refer to the upper object terreno.

Did you display with console.log() what were the values of this.xmin, this.subdivsions, etc just before calling CreateTiledGround() ?


It just looks like a classic javascript "this" scope issue.

Link to comment
Share on other sites

I think this should fix your problem as suggests Jerome :

var that = this;precision : {......// Create the Tiled Groundcreate : function(name, scene) { //"Tiled Ground"    	that.scene = scene;    	that.name = name;    	    	that.v = new BABYLON.Vector3(1, 10, 0);    	that.tiledGround = new BABYLON.Mesh.CreateTiledGround(name, that.xmin, that.zmin, that.xmax, that.zmax, that.subdivisions, that.precision, that.scene);    	}
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...