我在插入值的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;
}
答案 0 :(得分:2)
要在数组中插入新元素,可以使用方法splice()
,但首先,您必须创建要添加的对象(在代码中看起来像Point
) :
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);
答案 1 :(得分:0)
为什么不使用splice
函数?
array.splice( positionToInsertIn, 0, newValue );