views:

68

answers:

4

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"
+1  A: 

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));
Amarghosh
hi Amarghosh, thanks for the answer!unfortunately this solution doesn't work for me."Property homepage not found on com.website.Thumbs and there is no default value".my "homepage" value, is really only a string.in as2 i wrote:------------------------------------------------------function buttonClicked(nameOfTheButt){ thumbs.attachMovieClip(nameOfTheButt,this.getNextHighestDepth());}buttonClicked("hello");trace(thumbs.hello); //my movieclip------------------------------------------------------thanks again
Luca
is `homepage` the name of a clip exported for actionscript - can you do a `thumbs.addChild(new homepage());`
Amarghosh
no, mine example was not really right, im sorry.here you can find a really ( obviously simplified ) example.http://pastebin.com/q6rzyHGSthanks! :)
Luca
pastebin is blocked in my office; please edit the question and add the snippet there.
Amarghosh
Where is `thumbs_homepage` declared - inside any function or at the class level - is it a local variable? If not, `this[thumbToShow]` should work.
Amarghosh
thumbs_homepage is a private var declared at the class level.im declaring and istancing it, but i want to add it on the display list only when i click my button.based on the name of the button, i will addchild the related movieclip.
Luca
If you make it a `public` variable, you can use `thumbs.addChild(this["thumbs_homepage"]);`
Amarghosh
thanks a lot Amarghosh, you're the man!
Luca
+1  A: 
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

the binary
this solution seems to be really good for me...but how can i addchild if my movieclip are already istanced?you can find an example here :) thanks a lothttp://pastebin.com/q6rzyHGS
Luca
see the modified sample..
the binary
`getChildByName` will not work if he has added them to `this` movieclip, which doesn't seem to be the case here.
Amarghosh
A: 

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;
     }
  }
}
Cheapshot
A: 

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


kukutherobot