转换动画片段中的动态字符串名称以生成addchild

时间:2010-07-01 08:18:40

标签: flash actionscript-3 actionscript

我必须为动画片段制作一个addchild,但我真的不知道如何使用动态名称制作它。

例如

private function buttonClicked(nameOfTheButt:String):void
{
    thumbs.addChild(nameOfTheButt);
}

buttonClicked("homepage");

显然有一个转换错误,我试着让一个字符串的addchild ... 如何以快速和可能的清洁方式解决这个问题?


这是一个真实的例子,基于我得到的一些答案:

thumbs_homepage = new MovieClip();
thumbs_casehistory = new MovieClip();
thumbs_contacts = new MovieClip();


public function showThumbs(thumbsToShow:String):void
{
    //dynamic addchild
}

showThumbs("thumbs_homepage"); //or "thumbs_contacts" or "thumbs_casehistory"

4 个答案:

答案 0 :(得分:1)

如果"homepage"this对象的属性,则可以使用:

thumbs.addChild(this[nameOfTheBtn]);

如果是this对象的孩子,你可以这样做:

thumbs.addChild(this.getChildByName(nameOfTheBtn));

答案 1 :(得分:1)

thumbs_homepage = new MovieClip();
thumbs_homepage.name = "thumbs_homepage";

thumbs_casehistory = new MovieClip();
thumbs_casehistory.name = "thumbs_casehistory";

thumbs_contacts = new MovieClip();
thumbs_contacts.name = "thumbs_contacts";


private function buttonClicked (nameOfTheButt:String): void {

    var _instance: MovieClip = getChildByName(nameOfTheButt) as MovieClip;

    thumbs.addChild(_instance);
}


showThumbs(thumbs_homepage.name);

希望它有帮助.. 问候

答案 2 :(得分:0)

首先,您需要对包含这些动画片段的对象的引用。尝试以下解决方案。假设您正在编写文档类,如下所示:

package
{
  public class Main extends MovieClip
  {
     public var myMovie_1:MovieClip;
     public var myMovie_2:MovieClip;
     public var myMovie_3:MovieClip;

     public function Main():void
     {
        myMovie_1 = new MovieClip();
        myMovie_2 = new MovieClip();
        myMovie_3 = new MovieClip();

        this.addChildByInstanceName("myMovie_1");
     }

     public function addChildByInstanceName(p_objectInstanceName:String):DisplayObject
     {
        if(!this.contains(this["p_objectInstanceName"]))
        {
           this.addChild(this["p_objectInstanceName"]);
        } else {
           throw(new Error("You cannot add a child twice in the display list!"));
        }
        return this["p_objectInstanceName"] as DisplayObject;
     }
  }
}

答案 3 :(得分:0)

为什么不直接传递影片剪辑?假设“thumbs”是已存在的精灵或动画片段:

    
thumbs_homepage = new MovieClip();
thumbs_casehistory = new MovieClip();
thumbs_contacts = new MovieClip();

public function showThumbs(mc:MovieClip):void
{
    thumbs.addChild(mc);
}

// adds thumbs_homepage to thumbs
showThumbs(thumbs_homepage);