views:

71

answers:

1

Hello all, Thanks very much for your time! Here is my question,...

public function addNewMc():void{
    var newMC:MovieClip= new MovieClip();
    this.addChild(newMC);
}
public function removeOldMc(newMC):void{
    this.removeChild(newMC);
}

How can I create a new MovieClip within a method, which can be used throughout the class, without defining it at the top of the class? And for extra points, without using return.

I can get it to work, if the first function addNewMc returns the value newMC, and passing that to any other methods... but for what I am writing, I hope to use up my return with something else. Thanks!

A: 

Don't know if I'm understanding you completely but it sounds like you want access to a dynamically created Movieclip without explicitly defining it?! is that right? If so, then you could do what you have now but add a method for retrieval:

public function addNewMc():void{
    var newMC:MovieClip= new MovieClip();
    this.addChild(newMC);
}

public function getMC():MovieClip
{
    var len:uint = this.numChildren;
    while(len--)
    {
      var tempObj:* = this.getChildAt(len);
      if(tempObj is MovieClip)
         return MovieClip(tempObj);
     }
     return null;
}

You could also add a name property to the dynamically created movieclip:

public function addNewMc():void
{
    var newMC:MovieClip= new MovieClip();
    newMC.name = "new_MC";
    this.addChild(newMC);
}

you could then retrieve like this:

this.getChildByName("new_MC");

Again don't know if I'm understanding your exact requirements cheers erick ;)

erick