AS3 范围问题,如何在包/类中动态创建新的 MC?

发布于 2024-08-20 16:50:22 字数 393 浏览 2 评论 0原文

非常感谢您抽出时间!这是我的问题,...

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

如何在方法中创建一个新的 MovieClip,该方法可以在整个类中使用,而无需在类的顶部定义它?并且为了加分,不使用退货。

如果第一个函数 addNewMc 返回值 newMC ,并将其传递给任何其他方法,我就可以让它工作......但对于我正在写的内容,我希望用其他东西来用完我的返回值。谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无边思念无边月 2024-08-27 16:50:22

不知道我是否完全理解您的意思,但听起来您想要访问动态创建的影片剪辑而不显式定义它?!对吗?
如果是这样,那么您可以执行现在的操作,但添加一个检索方法:

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

您还可以向动态创建的影片剪辑添加一个名称属性:

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

然后您可以像这样检索:

this.getChildByName("new_MC");

再次不知道我是否理解您的确切意思要求
干杯
埃里克;)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文