我编写了一个返回值的脚本。当我记录包含值的变量时,它会显示它们,并且它们是正确的。然后,无论我如何尝试,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相同。
他们已被宣布,他们只是在一个不同的对象。
我无法理解为什么它不会将值添加到数组中。我记录它们并查看它们,然后在调用数组推送后直接进行,并将所有内容设置为undefined。为什么会这样?
他们被试图让他们进入阵列的唯一原因是因为这是我尝试的最后一件事。
答案 0 :(得分:0)
我相信我遇到了冲突的变量名称的问题,我重命名了其中的一些并开始工作。