我测试电影时出现此错误: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";
我尝试使用对象而不是数组,但我得到了同样的错误。
答案 0 :(得分:1)
这是因为pieceArr
未定义。
另外(即使您之前已定义pieceArr
,pieceArr[1]
也不存在。请注意,在您尝试访问{{1}之前,尚未向pieceArr[1]
分配任何内容}和x
。
此代码可以完美运行。
y
答案 1 :(得分:0)
您在创建pieceArr
数组之前引用它。因此,当您尝试设置wRook1.mc.x
和wRook1.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;