我正在编写一个具有一些独特属性的地形生成器,但我一直在努力解决范围问题。这可能看起来像一个菜鸟问题,但我搜索了档案,我没有找到任何能够解决这个问题的东西。我在函数中有一个函数(我不熟悉这个对象范例),我试图从另一个对象访问一个内部函数。在代码中,我在Chrome控制台中收到一条错误,指出“Uncaught TypeError:无法读取未定义的waxy.js的属性'宽度':45 BoundingBox waxy.js:45(匿名函数)”错误发生在调用'时的最后一行' bounds.width'我需要公开这个函数但是怎么做?
以下是相关代码:
//A wrapper class for an int triple
var 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 prism
var 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 set
var MapData = (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 drops
var 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[i] = Math.random() <= rate
}
return drop
}
})//end DropSheet
//simulated annealing
var 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 = MapData(bounds, propertyList)
//create a drop sheet
var sheetMaker=DropSheet({width:bounds.width, depth:bounds.depth, rate:fillPercent})
//some functions...
}
答案 0 :(得分:1)
创建BoundingBox时,您应该使用new
关键字,例如opts.bounds || new BoundingBox([{x:0,y:0,z:0}, {x:80,y:80,z:80}])
。另外,要将对象的属性设置为public而不是private,您需要将它们分配给this
,如下所示:
this.width = function() {return this.upperBound.x - this.lowerBound.x}
最后,当使用width
时,你需要调用它,因为它是一个函数; bounds.width()
。你可以把它变成一个变量(如果它不会改变),就像这样:
this.width = this.upperBound.x - this.lowerBound.x;
但很明显,如果upperBound或lowerBound发生变化,变量不会随之变化。