简体   繁体   中英

AS3 Scope issue, How do I dynamically create a new MC in a package/class?

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM