嗯,这有点模糊。这些对象在哪里实例化?我建议确保它们在您的课程中被实例化:
package
{
public class Document extends Sprite //I don't think you really need MovieClip
{
//alternatively to the method bellow you could use something like:
//
// private var object1 : Object = new Object();
// private var object2 : Object = new Object();
// public var obj : Array = [object1, object2];
//
// just make sure they are instantiated before they are used in the obj contructor
public var obj : Array = [new Object(),new Object()]; //again here I would suggest using a vector if they are the same type
public function Document()
{
trace(obj[0]);
}
}
}
如果这些对象在类外部,我建议将它们传递给构造函数,如下所示:
package
{
public class Document extends Sprite //I don't think you really need MovieClip
{
public var obj : Array = [null,null]; //again here I would suggest using a vector if they are the same type
public function Document(o1:Object=null,o2:Object=null)
{
if (o1 != null)
obj[0] = o1;
if (o2 != null)
obj[1] = o2;
//
if (obj[0] != null)
trace(obj[0]);
else
trace("Obj[0] is null");
}
}
}
[稍后编辑] 至于发生这种情况的原因是因为在数组初始化时这两个是空的(它们还没有被初始化)
[稍后编辑2] 好的 - Document 是 flash 的根类 - 很高兴知道就像我在评论中所说的那样,即使在舞台上这两个对象在添加到舞台之前不会被实例化。为此,我建议收听 ADDED_TO_STAGE 事件。如果您将它们传递到构造函数之外,则在创建数组时它们将为空,因为它们尚未添加到阶段/创建(与普遍的看法相反,即使在 Flash 中,对象也不会简单地存在)