javascript cannot read property 'width' of undefined

keniz91

New Member
I'm writing a terrain generator with some unique properties but I've been struggling with a scope issue. This may seem like a noob issue but I've searched the archives and I haven't found anything that illuminates the problem for me. I have a function within a function (I'm not familiar with this object paradigm) and I'm trying to access an inside function from another object. In the code I get an error in Chrome console that says "Uncaught TypeError: Cannot read property 'width' of undefined waxy.js:45 BoundingBox waxy.js:45 (anonymous function)" The error occurs at the last line when calling 'bounds.width' I need to expose this function but how?Here is relevant code:\[code\]//A wrapper class for an int triplevar Point = (function(dimensions) { var x = function() {return dimensions.x || 0} var y = function() {return dimensions.y || 0} var z = function() {return dimensions.z || 0}})//end Point//a working set, rectangular prismvar BoundingBox = (function(dimensions) { var lowerBound = function(){return Point(dimensions[0])} var upperBound = function(){return Point({x:dimensions[1].x + dimensions[0].x, y:dimensions[1].y + dimensions[0].y, z:dimensions[1].z + dimensions[0].z})} var width = function() {return this.upperBound.x - this.lowerBound.x} var depth = function() {return this.upperBound.y - this.lowerBound.y} var height = function() {return this.upperBound.z - this.lowerBound.z} var contains = function(cOffset, cDims) { return !((cOffset.x < this.lowerBound.x || cOffset.y < this.lowerBound.y || cOffset.z < this.lowerBound.z) || (cDims.x > this.upperBound.x || cDims.y > this.upperBound.y || cDims.z > this.upperBound.z)) } var intersects = function(iOffset, iDims) { return ((iOffset.x >= this.lowerBound.x && iOffset.y >= this.lowerBound.y && iOffset.z >= this.lowerBound.z) || (iDims.x <= this.upperBound.x && iDims.y <= this.upperBound.y && iDims.z <= this.upperBound.z)) } var iterator = function() { return BoxIterator(this) }})//end BoundingBox//stores terrain value for each x,y,z in a working set (BoundingBox)//each property in a list has a value for each node in working setvar MapData = http://stackoverflow.com/questions/15601478/(function(workingSet, properties) { var bounds = workingSet var props = properties var data = [] //make sure property name is spelled right var map = function(x,y,z,property) {return data[x,y,z,props.indexOf(property)] } var terrainMap = function(x,y,z) {return map(x,y,z,'terrain')} //!isEmpty equiv. isTerrain var isEmpty = function(x,y,z) {return map(x,y,z,'terrain')===0} var tempMap = function(x,y,z){return map(x,y,z,'temperature')}})//end MapData//create a sheet of terrain dropsvar DropSheet = (function(opts) { var depth = opts.depth var width = opts.width var rate = opts.rate var generate = function() { for(var i = 0; i<depth * width; i++) { drop = Math.random() <= rate } return drop }})//end DropSheet//simulated annealingvar WaxyGenerator = (function(opts) { var initialTemp = opts.initialTemp || 1.0 var fillPercent = opts.fillPercent || 0.35 var conductivity = opts.conductivity || 0.04 var freezing = opts.freezing || 0.1 var floorTemp = opts.floorTemp || 0.0 var bounds = opts.bounds || BoundingBox([{x:0,y:0,z:0}, {x:80,y:80,z:80}]) //each property in propertyList has its own map data var propertyList = opts.propertyList || ['terrain', 'temperature'] var materials = opts.materials || ['grass', 'dirt', 'grass_dirt', 'obsidian', 'whitewool', 'brick'] //map of data var data = http://stackoverflow.com/questions/15601478/MapData(bounds, propertyList) //create a drop sheet var sheetMaker=DropSheet({width:bounds.width, depth:bounds.depth, rate:fillPercent}) //some functions...}\[/code\]
 
Back
Top