开发者

convert a dynamic string name in a movieclip to make an addchild

i have to make an addchild to a movieclip, but i really don't know how to make it with a dynamic name.

for example

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

buttonClicked("homepage");

obviously there's a casting error, im tryin to make an addchild to a string... how can i solve this problem with a fast and possibily clean way?


this is the real example, based on some answers i get:

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"开发者_运维百科


If "homepage" is a property of this object, you can use:

thumbs.addChild(this[nameOfTheBtn]);

If it is a child of this object, you can do:

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


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);

hope it helps.. regards


First, you need the reference to the object that contains these movieclips. Try the solutions below. Let's say that you're coding your document class like this:

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;
     }
  }
}


Why not just pass the movieclip directly? Assuming "thumbs" is sprite or movieclip that already exists:

    
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);


0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜