我正在编写一个具有一些独特属性的地形生成器,但我一直在努力解决范围问题。这似乎是一个菜鸟问题,但我已经搜索了档案,但没有找到任何可以说明问题的东西。我在一个函数中有一个函数(我不熟悉这个对象范例),我试图从另一个对象访问一个内部函数。在代码中,我在 Chrome 控制台中收到一个错误,上面写着“未捕获的 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...
}