现在的位置: 首页 > 综合 > 正文

ActionScript3中removeChildAt()的使用注意事项

2014年03月26日 ⁄ 综合 ⁄ 共 718字 ⁄ 字号 评论关闭

在AS3中我们删除一个容器的子项通常使用 removeChildAt(i:int)这个方法,当我们想要完成清空容器的功能时,通常我们都会使用循环实现。务必要注意的是:如果我们删除一个子项,Container 中该子项之上的任何显示对象的索引位置都减去 1,也就是说AS3中会自动调整所有的子对象的深度,不能跨深度。

下面我会提供几种解决方案:

1.实现正序删除:

 var numChild:int = this.panel.numChildren;
    while(this.panel.numChildren>=1){
     this.panel.removeChildAt(0)

    }

2.实现逆序删除:

var numChild:int =this.panel.numChildren;
     for(var i:int=numChild-1;i>=0;i--){
      this.panel.removeChildAt(i);
     }

3.实现逐个删除的动画效果:

this.panel.addEventListener(Event.ENTER_FRAME,clear_graphics);

    private function clear_graphics(evt:Event):void { 
      if (this.panel.numChildren==0) {
       this.panel.removeEventListener(Event.ENTER_FRAME,clear_graphics);
       }
       else{
       this.panel.removeChildAt(0);
       }
     }

抱歉!评论已关闭.