尋址不是這樣工作的。你不能用一個[]操作來解決孩子的孩子。此外,最好使用getChildByName方法,因爲點語法訪問僅適用於發佈選項「自動聲明階段實例」。
for (var u: int = 0; u < 4;u++)
{
for (var v: int = 1; v < 4;v++)
{
// Get a reference to a child and typecast it as MovieClip.
var aDigit:MovieClip = problem.getChildByName("digit" + u + "" + v) as MovieClip;
aDigit.gotoAndStop(1);
}
}
讓我們解釋一些事情。
首先,影片剪輯是一個動態類,這意味着你可以讀取和寫入其實例成員實際上不聲明它們(點和括號的語法之間的區別就在這裏解釋Using . or [ ] to access Object properties - what's the difference?):
var M:MovieClip = new MovieClip;
trace(M['a']); // undefined, no error raised
M['a'] = 1;
trace(M['a']); // 1
trace(M.a]); // 1
其次,影片剪輯是DisplayObjectContainer因此它可以包含子項。這些孩子有名字,你可以通過調用getChildByName(「child name goes here」)方法來引用它們,並且不要忘記對結果進行類型轉換,因爲有各種各樣的孩子,所以它們默認由其基類DisplayObject類輸入。孩子的名字是不一回事對象成員名稱(althougt,正如我前面提到的,「自動聲明舞臺實例」分配與幕後的相同名稱的變量子引用):
// We proceed working with M from above.
// Lets create a child for M.
var C:MovieClip = new MovieClip;
// MovieClip C has a name "D" as of now.
C.name = "D";
// C named "D" becomes a child of M.
M.addChild(C);
trace(M.C); // undefined, because C is not a member of M object, it's just a local variable
trace(M.D); // undefined, because D is the name of MovieClip which is child of M MovieClip but not a member of M objest.
trace(M.getChildByName("C")); // null, because M has no children with name "C".
trace(M.getChildByName("D")); // [object MovieClip] because there is indeed a child with the name "D" inside of M.
// We create a field with the name "E" inside M object.
M.E = C;
trace(M.getChildByName("E")); // null, because M has no children with name "E".
trace(M.E); // [object MovieClip] because M now contains a field E with the reference to MovieClip C named "D".
謝謝Organis,完美的工作! –
@MichaelM不要忘記標記正確的答案。 :) – Aaron
@MichaelM經過一番思考,我添加了一些解釋,這些解釋可能有助於你理解這些東西的工作原理。 – Organis