AS3使用一个变量来加载所有movieclip

时间:2012-04-11 04:43:58

标签: arrays actionscript-3 flash

有没有办法让我想在舞台上加载的所有movieclip成为一个数组?

if (selectDiffText.text == "Collection 1 Easy")
    {
        var c1_easy:cartoonEasy = new cartoonEasy();
        addChild(c1_easy);
        c1_easy.x = 412;
        c1_easy.y = 400;
        TweenMax.from(c1_easy, 0.5, {alpha:0, ease:Expo.easeOut});
    }
    else if (selectDiffText.text == "Collection 1 Medium")
    {
        var c1_medium:cartoonMedium = new cartoonMedium();
        addChild(c1_medium);
        c1_medium.x = 412;
        c1_medium.y = 400;
        TweenMax.from(c1_medium, 0.5, {alpha:0, ease:Expo.easeOut});
    }
    else if (selectDiffText.text == "Collection 1 Hard")
    {
        var c1_hard:cartoonHard = new cartoonHard();
        addChild(c1_hard);
        c1_hard.x = 412;
        c1_hard.y = 400;
        TweenMax.from(c1_hard, 0.5, {alpha:0, ease:Expo.easeOut});
    }
else{trace("ERROR!")}

现在我在图书馆里有3个动画片夹,里面有卡通片,卡通片和卡片片。 我会将其中一个添加到舞台中但使用不同的变量。 这是一种我只能使用ONE变量的方法吗?它是否使用数组?

因为如果只使用1个变量,那么我可以很容易地控制我从库中调用的movieclip中的1个对象。

我在想的是这样的:

var c1_all:cartoonAll = new cartoonEasy() = new cartoonMedium() = new cartoonHard() 

2 个答案:

答案 0 :(得分:1)

function addCartoon( obj:* ):void{ // might change * to DisplayObject
    addChild(obj);
    obj.x = 412;
    obj.y = 400;
    TweenMax.from(obj, 0.5, {alpha:0, ease:Expo.easeOut});
}

var obj:*;
switch (selectDiffText.text ){ // use switch since it is clean code
  case "Collection 1 Easy":
    obj = new cartoonEasy();
  case "Collection 1 Medium":
    obj = new cartoonMedium();
  case "Collection 1 Hard":
    obj = new cartoonHard();
  default:
    trace("ERROR!")
}
if( obj ){
  addCartoon( obj );
}






// manipulating obj will change the item on stage.
obj.x = 500;

答案 1 :(得分:0)

确实可以使用数组。

var c1All:Array = [
    new cartoonEasy(),
    new cartoonMedium(),
    new cartoonHard()
];

要将代码应用于所有这些对象,您可以执行以下操作:

for each(var i:MovieClip in c1All)
{
    addChild(i);
    i.x = 412;
    i.y = 400;
    TweenMax.from(i, 0.5, {alpha:0, ease:Expo.easeOut});
}

PS我建议使用throw new Error("message");而不是trace("error")