创建对象或数组时出错#1009

时间:2011-12-08 10:09:38

标签: actionscript-3

我测试电影时出现此错误:TypeError:错误#1009:无法访问空对象引用的属性或方法。

我的代码是:

var pieceArr:Array = new Array();
pieceArr[1] = {
    piece: MovieClip(wRook1.mc),
    x: 34,
    y: 526
}

var wRook1:Object = new Object();
wRook1.mc = new WhiteRook();
addChild(wRook1.mc);
wRook1.mc.x = pieceArr[1].x;
wRook1.mc.y = pieceArr[1].y;
wRook1.inPeace = "a1";

我尝试使用对象而不是数组,但我得到了同样的错误。

2 个答案:

答案 0 :(得分:1)

这是因为pieceArr未定义。

另外(即使您之前已定义pieceArrpieceArr[1]也不存在。请注意,在您尝试访问{{1}之前,尚未向pieceArr[1]分配任何内容}和x

此代码可以完美运行。

y

答案 1 :(得分:0)

您在创建pieceArr数组之前引用它。因此,当您尝试设置wRook1.mc.xwRook1.mc.y时,您会尝试获取尚不存在的内容。您必须将代码的顺序更改为以下内容:

var wRook1:Object = new Object();
wRook1.mc = new WhiteRook();
addChild(wRook1.mc);

wRook1.inPeace = "a1";

var pieceArr:Array = new Array();
pieceArr[1] = 
{
    // can't reference wRook1.mc before it's created
    piece: MovieClip(wRook1.mc),
    x: 34,
    y: 526
}

// can't reference pieceArr before it's created
wRook1.mc.x = pieceArr[1].x;
wRook1.mc.y = pieceArr[1].y;