うーん、これはちょっと曖昧です。これらのオブジェクトはどこでインスタンス化されますか? クラスでインスタンス化されていることを確認することをお勧めします。
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つがnullであるためです(まだ初期化されていません)
[LATER EDIT2] OK - ドキュメントはフラッシュのルート クラスです - 知っておくとよい コメントで述べたように、ステージではこれら 2 つのオブジェクトはステージに追加されるまでインスタンス化されません。そのためには、ADDED_TO_STAGE イベントを聞くことをお勧めします。それらをコンストラクターの外に渡すと、それらはステージ/作成済みにまだ追加されていないため、配列が作成されるとnullになります(一般的な信念に反して、Flashでもオブジェクトは単に存在しません)