Flex AS3:从特定类的容器中查找并删除元素

发布于 2024-12-17 18:42:37 字数 323 浏览 4 评论 0原文

如何仅删除在包含 Textinputs 和 Buttons 的 Bordercontainer 中找到的每个图像?

我尝试过:

for(var i:int=0;i<container.numElements;i++){
if(container.getElementAt(i) is Image){
container.removeElementAt(i);}
}

但正如预期的那样,这个循环并没有完全工作,因为 numElements 发生了变化,这意味着并非所有图像都会被删除。 我知道这有一些简单的技巧...但我现在想不起来...请帮助

How can I remove only every image found in a Bordercontainer which also holds Textinputs and Buttons ?

i tried:

for(var i:int=0;i<container.numElements;i++){
if(container.getElementAt(i) is Image){
container.removeElementAt(i);}
}

But as expected this loop does not fully work since the numElements changes which means that not all Images get removed.
I know there is some simple trick to this...but I cant think of it right now...Please help

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

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

发布评论

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

评论(2

请远离我 2024-12-24 18:42:37

正如评论者所建议的,似乎向后循环是实现这一目标的方法。我会尝试这样的事情:

var totalElements : int = container.numElements;
for(var i:int=totalElements-1;i>=0;i--){
 if(container.getElementAt(i) is Image){
  container.removeElementAt(i);
 }
}

通过在开始循环之前将 numElements 存储在变量中,您可以确保该值在处理循环时不会改变。既然你倒退了,你就不必担心子索引的变化。

第二个选项是在一个循环中对图像实例进行排队,并在第二个循环中使用removeElement 方法将它们删除。我怀疑两个循环方法的性能会明显较差。

As commenters have suggested, it seems like looping backwards would be the way to do it. I'd try something like this:

var totalElements : int = container.numElements;
for(var i:int=totalElements-1;i>=0;i--){
 if(container.getElementAt(i) is Image){
  container.removeElementAt(i);
 }
}

By storing the numElements in a variable before starting the loop, you can be sure that the value will not change while processing the loop. Since your going backwards, you don't have to worry about the child index changing.

A second option would be to queue up the image instances in one loop and remove them in a second loop using the removeElement method. I suspect the two loop method will have significantly worse performance.

朕就是辣么酷 2024-12-24 18:42:37

向后循环是实现此目的的一种方法。

另一个是

for(var i:int=0; i<container.numElements; i++){
    if(container.getElementAt(i) is Image){
        container.removeElementAt(i);
        i--; //This nullifies the effect of removing an element
    }
}

Looping backwards would be 1 way to do this.

Another would be

for(var i:int=0; i<container.numElements; i++){
    if(container.getElementAt(i) is Image){
        container.removeElementAt(i);
        i--; //This nullifies the effect of removing an element
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文