0

有没有办法让我想在舞台上加载的所有影片剪辑成一个数组?

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 个电影剪辑,分别是 cartoonEasy、cartoonMedium 和 cartoonHard。我会将其中一个添加到舞台中,但变量不同。这是一种我可以让他们只使用一个变量的方法吗?它使用数组吗?

因为如果只使用 1 个变量,那么我可以轻松地控制我从库中调用的影片剪辑的 1 内的对象。

我的想法是这样的:

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

2 回答 2

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;
于 2012-04-11T21:35:20.473 回答
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").

于 2012-04-11T05:05:27.090 回答