[英]AS3 - Why is the class array not working properly?
I have a class named tOne. 我有一个名为tOne的课程。 The class code is below.
类代码如下。
public class tOne extends MovieClip {
private var _root:MovieClip;
public var tPath:Array = new Array();
public var index:int = 0;
public function tOne() {
this.addEventListener(Event.ADDED, beginClass);
this.addEventListener(Event.ENTER_FRAME, gameLoop);
}
private function beginClass(e:Event):void {
_root = MovieClip(root);
tPath = _root.tMovingPath; //Sets the tPath array to a bunch of coordinates
}
private function gameLoop(e:Event):void {
if (tPath[index] != null) {
this.x = tPath[index].xCoord;
this.y = tPath[index].yCoord;
tPath.splice(index, 1); //Here is the problem
index++;
}
}
}
I am create three instances of this class, so I have 3 tOne objects. 我将创建此类的三个实例,因此我有3个tOne对象。 Now my problem is, when I use the 'tPath.splice(index, 1)', it does not remove that index from one of the tOne objects, but from all three objects.
现在我的问题是,当我使用'tPath.splice(index,1)'时,它不会从tOne对象之一中删除该索引,而是从所有三个对象中删除该索引。
So if in the first object of tOne I have an array length of 3 and remove one of them, it removes one from the other two objects of tOne. 因此,如果在tOne的第一个对象中,我的数组长度为3并删除其中一个,它将从tOne的其他两个对象中删除一个。
I don't understand why. 我不明白为什么。
Can anyone explain to me what is happening? 谁能告诉我发生了什么事?
In your line 在你的行
tPath = _root.tMovingPath;
you are setting tPath to reference an array. 您正在设置tPath来引用数组。 The references of all three objects will point to the same array.
所有三个对象的引用将指向同一数组。
What you need to do is make a copy of that array. 您需要做的是复制该数组。 One way of doing it:
一种方法是:
tPath = _root.tMovingPath.concat();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.