0

我在 3 位置插入一个值,该值被插入但不知何故在复制其余部分时它不会复制最后一点。数组的大小没有增加。谁能告诉我如何在中间的数组中添加新元素。

for(indexpoint=0;indexpoint<3;indexpoint++) 
{           
    temp.points[indexpoint].x = intpoints[indexpoint].x+this.x;
    temp.points[indexpoint].y = intpoints[indexpoint].y+this.y; 
}

temp.points[3].x = (intpoints[2].x+intpoints[3].x)/2+this.x;
temp.points[3].y = (intpoints[2].y+intpoints[3].y)/2+this.y;

for(indexpoint=3;indexpoint<intpoints.length;indexpoint++)
{           
    temp.points[indexpoint+1].x = intpoints[indexpoint].x+this.x;
    temp.points[indexpoint+1].y = intpoints[indexpoint].y+this.y;       
}
4

2 回答 2

2

要在数组中插入新元素,您可以使用 方法splice(),但首先,您必须创建要添加的对象(它Point在您的代码中看起来像 a):

const point:Point = new Point();
point.x = intpoints[2].x+intpoints[3].x)/2+this.x;
point.y = intpoints[2].y+intpoints[3].y)/2+this.y;

temp.points.splice(3, 0, point);

你也可以这样做:

temp.points.length = 0;

for each (var point:Point in intpoints) {
    temp.points.add(point.clone().add(this));
}

const newPoint:Point = new Point();
newPoint.x = intpoints[2].x+intpoints[3].x)/2+this.x;
newPoint.y = intpoints[2].y+intpoints[3].y)/2+this.y;
temp.points.splice(3, 0, newPoint);
于 2013-02-13T22:33:48.003 回答
0

为什么不直接使用splice函数?

array.splice( positionToInsertIn, 0, newValue );
于 2013-02-13T22:37:40.133 回答