2011-04-24 85 views
0

你好我試圖做到這一點(在as2這個工作,但不是在as3)我看了谷歌3小時,但仍然沒有找到一個解決方案(thans你的幫助):AS3動態變形動畫片短片

import flash.display.MovieClip; 

var mcContainer:MovieClip = new MovieClip(); 

var mcImage0:MovieClip = new MovieClip(); 
var mcImage1:MovieClip = new MovieClip(); 
var mcImage2:MovieClip = new MovieClip(); 
var mcImage3:MovieClip = new MovieClip(); 

mcImage0.name = "Boy"; 
mcImage1.name = "Girl"; 
mcImage2.name = "Woman"; 
mcImage3.name = "Man"; 


var ArrayNamesOfMC:Array = new Array(); 

var i:int = 4; 

while(i--) { 
    ArrayNamesOfMC.push(["mcImage"+i].name); 
} 

這donsn't工作:

ArrayNamesOfMC.push(["mcImage"+i].name); 

回答

1

這應該做的伎倆:

var _movieClip:MovieClip = ("mcImage" + i) as MovieClip; 
ArrayNamesOfMC.push(_movieClip.name); 
2

這是一個簡單的回答你的問題:

var mcImage0:MovieClip = new MovieClip(); 
var mcImage1:MovieClip = new MovieClip(); 
var mcImage2:MovieClip = new MovieClip(); 
var mcImage3:MovieClip = new MovieClip(); 

mcImage0.name = "Boy"; 
mcImage1.name = "Girl"; 
mcImage2.name = "Woman"; 
mcImage3.name = "Man"; 

var ArrayNamesOfMC:Array = new Array(); 

var i:int = 3; 

while (i >= 0) 
{ 
    ArrayNamesOfMC.push(MovieClip(this["mcImage" + i]).name); 

    i--; 

}// end while 

下可能不是你的情況相關,因爲我不太清楚你的應用程序的目的是什麼,但是這可能是一個更好的方法:

var sprites:Vector.<Sprite> = new Vector.<Sprite>(); 
var names:Vector.<String> = new <String>["Boy", "Girl", "Woman", "Man"]; 

for (var i:uint = 0; i < names.length; i++) 
{ 
    var sprite:Sprite = new Sprite(); 
    sprite.name = names[i]; 

    sprites.push(sprite); 

}// end for 

忽略此,如果它不適用於您的情況。

1

Taurayi的答案是一個我不知道的有趣技巧。

個人而言,我會建議重組你的代碼,把所有的影片剪輯中的數組,像這樣:

var mcImages:Array = new Array(); 
for (var i = 0; i < 4; i++) { 
    mcImages.push(new MovieClip); 
} 
mcImages[0].name = "Boy"; 
trace(mcImages[0].name); 

順便說一句,你的while循環,構建正確。在圓括號中需要一個條件,然後在循環內進行遞減。但是將所有的影片剪輯放在一個數組中,那麼您可以使用這個更簡單的方法來遍歷所有這些:

for each (var mc in mcImages) { 
    trace(mc.name); 
}