如何从舞台上删除一堆动态创建的东西?

发布于 2024-10-01 02:44:35 字数 339 浏览 4 评论 0原文

我试图一次性从舞台上删除一堆不同的东西。 我有 3 个动态创建的文本字段和 2 个动态创建的影片剪辑。 我通过我的文档类将它们添加到舞台上,购买创建它们,编辑它们的属性,然后...

addChild(myText1);
addChild(myText2);
addChild(myText3);
addChild(myMovieClip1);
addChild(myMovieClip2);

我想将它们全部删除,我已经尝试过...

removeChild(myText1); 

等等

但这不起作用。 谁能帮助我。

I am trying to delete a bunch of different things from the stage all at once.
I have 3 dynamically created text fields and 2 dynamically created movie clips.
I added them to the stage through my document class buy creating them, editing their properties and then...

addChild(myText1);
addChild(myText2);
addChild(myText3);
addChild(myMovieClip1);
addChild(myMovieClip2);

I want to delete them all and I have tried...

removeChild(myText1); 

ETC

But this doesn't work.
can anyone help me.

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

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

发布评论

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

评论(3

深居我梦 2024-10-08 02:44:35

如果您想从当前对象中删除所有内容,您可以执行以下操作:

while (numChildren > 0) removeChildAt(0);

如果您的 DisplayObjects 在舞台上,您可以执行以下操作:

stage.removeChild(myText1);

If you want to remove everything from the current object, you can do :

while (numChildren > 0) removeChildAt(0);

If your DisplayObjects are on the stage, you can do :

stage.removeChild(myText1);
七堇年 2024-10-08 02:44:35

如果您只想删除这些特定对象,那么您需要将对它们的引用存储在某处。

另一种解决方案是在创建每个对象时填充每个对象的 .name 属性,然后在将来使用该名称来获取对该对象的引用:

var __dynamicMovieClip:MovieClip = new MovieClip();
__dynamicMovieClip.name = "foo";

addChild(__dynamicMovieClip);

然后在稍后的某个时间:

removeChild(getChildByName("foo"));

getChildByName 会带来相当多的开销不过,所以重复调用它确实不是一个好主意,或者在当前显示列表很复杂/很深的情况下

If you only want to be able to delete those specific objects, then you'll need to store references to them somewhere.

An alternative solution would be to populate the .name property of each object when you create it, and then use that name at a future time to grab a reference to the object:

var __dynamicMovieClip:MovieClip = new MovieClip();
__dynamicMovieClip.name = "foo";

addChild(__dynamicMovieClip);

And then at some later time:

removeChild(getChildByName("foo"));

getChildByName carries quite a bit of overhead though, so it's really not a good idea to call it repeatedly, or in a situation where the current display list is complex/deep

掩于岁月 2024-10-08 02:44:35

您需要为每个动态创建的对象拥有成员变量,以便您可以在用于创建它们的函数之外引用它们。如果您只需要一个引用将它们从舞台上删除,那么数组会很好用。

private var objectsToDelete : Array = [];

private function someFunction ( ) : void
{
    objectsToDelete.push(myText1);
    objectsToDelete.push(myText2);
    objectsToDelete.push(myText3);
    objectsToDelete.push(myMovieClip1);
    objectsToDelete.push(myMovieClip2);
}

private function removeObjects () : void
{
    var i : int = 0;
    var max : int = objectsToDelete.length;
    for ( i; i < max; i++ )
    {
        removeChild( objectsToDelete[ i ] );
    }
}

You need to have member variables for each of the dynamically created objects so you can reference them outside of the function you used to create them. If you only need a reference to remove them from the stage, an array will work great.

private var objectsToDelete : Array = [];

private function someFunction ( ) : void
{
    objectsToDelete.push(myText1);
    objectsToDelete.push(myText2);
    objectsToDelete.push(myText3);
    objectsToDelete.push(myMovieClip1);
    objectsToDelete.push(myMovieClip2);
}

private function removeObjects () : void
{
    var i : int = 0;
    var max : int = objectsToDelete.length;
    for ( i; i < max; i++ )
    {
        removeChild( objectsToDelete[ i ] );
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文