我写了一个返回值的脚本。当我记录包含值的变量时,它将显示它们,并且它们是正确的。然后,无论我如何尝试,当我尝试将值添加到数组中时,进行concat,push,equals等操作,然后立即记录该数组的内容,这些值不存在或未定义。这是我的代码,或者至少是它的一个子集。
displayRange: function () {
if( app.selectedObject !== undefined && app.selectedObject.movement !== undefined && app.selectActive === undefined ) {
// holds rage
t.rArray = [];
t.oArray = [];
app.selectActive = true;
t.obj = app.selectedObject;
// amount of allotted movement for unit
t.len = t.obj.movement * 2;
// loop through x and y axis
for ( x = -t.len; x < t.len; x += 1){
for ( y = -t.len; y < t.len; y += 1 ){
// return cost of each movement
t.hl = this.calcRange('x', x, y );
// if movement cost is less then or eual to the allotted movement then add it to the range array
if ( t.hl.move <= t.obj.movement ){
// find obsticles
t.ex = t.hl.origin.x + x;
t.wy = t.hl.origin.y + y;
t.obsticle = this.findObsticles(t.ex, t.wy);
// get the number of offset movement from the obsticle
if ( t.obsticle !== undefined ){
t.offset = app.settings.obsticleStats[t.obsticle.obsticle][app.selectedObject['type']];
if ( t.offset !== undefined ){
// make an array of offset values, starting point, plus movement, and the amount of offset beyond that movement
t.oArray.push(this.obsticleLogic( t.ex, t.wy, t.mx, t.my, t.offset ));
}
}
// add all values to array
t.rArray.push({ x: t.ex, y: t.wy, type:'highlight'});
}
}
}
offsetArr = app.offsetArray( t.rArray, t.oArray );
for ( l = 0; l < offsetArr; l += 1 ){
app.map.highlight.push(offsetArr[l]);
}
for ( i = 0; i < t.oArray.length; i += 1 ){
app.offset.push(t.oArray[x]);
}
console.log(t.oArray);
console.log(offsetArr);
console.log('offset:');
console.log(app.offset);
console.log('highlight');
console.log(app.map.highlight);
window.requestAnimationFrame(app.animateEffects);
}
return false;
},
我已经设置了app.map.highlight = []; 在app.map区域中。与app.offset相同。
它们已经被声明,它们只是在另一个对象中。
我不明白为什么它不会将值添加到数组。我记录了它们并看到了它们,然后在调用数组推送之后立即将其设置为未定义。为什么会这样?
它们被循环遍历以尝试将它们放入数组的唯一原因是因为这是我最后尝试过的方法。
我相信我遇到了变量名冲突的问题,我将其中一些重命名,然后它开始工作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句