Flash AS3 - 删除多个 addChild

Flash AS3 - Remove Multiple addChild

我正在使用以下方法添加重复动画。

var recC01: cube01;
function attachC1() {
    recC01 = new cube01();
    recC01.x = -158;
    recC01.y = 159;
    addChild(recC01);
    trace("cube1");
}
var myIntervalC1: uint = setInterval(attachC1, 500);

下面是移除动画的代码:

btnBACK.addEventListener (MouseEvent.CLICK, back1);
function back1(event:MouseEvent) :void {
    gotoAndPlay(2);
var removeTimer;    
clearInterval(myIntervalC1);
recC01.parent.removeChild(recC01);
    trace ("back1") ;

}

在具有唯一名称的各种关键帧处添加重复动画。 IE。 recC01、recC02 等以及 attachC1、attachC2 等

在播放过程中,用户可以单击 "Back" 按钮返回到上一节。我很挣扎,因为按钮出现在实例之前的时间线上,所以我不能对尚不存在的实例使用 clearInterval 和 Removechild 。我想避免每次添加新的 child 时都创建唯一的 var。跟踪那个会很疯狂。

用户可以在加载任何这些之前或在 recC01 和 recC02 之间单击后退按钮...

删除所有 child 实例的最佳方法是什么? 有没有一种方法可以监听 Interval 和 Child 并将它们全部删除?

This a small screen capture of the animation. The boxes represent the liquid flow

更新 我想我会在这里更新而不是在评论中更新。 我已将此添加到第 2 帧:

var cubeContainer:Sprite = new Sprite();
addChild(cubeContainer);

重复加载的代码animation.class

var recC01: cube01;
function attachC1() {
    recC01 = new cube01();
    recC01.x = -158;
    recC01.y = 159;
    cubeContainer.addChild(recC01);
    trace("cube1");
}
var myIntervalC1: uint = setInterval(attachC1, 500);

后退按钮代码:

btnBACKp1.addEventListener(MouseEvent.CLICK, back1);
function back1(event: MouseEvent): void {
cubeContainer.removeChildren();
gotoAndPlay(2);
trace("backFrame2");    
}

卸载一秒钟,然后继续加载。这可能是间隔的结果吗?以前卸载单个实例的代码,我不得不删除Interval

您可以通过以下几种方法使此操作更简洁:

1。使用容器

在您的代码中的某处(在执行所示代码之前)创建一个容器来容纳您的所有多维数据集:

var cubeContainer:Sprite = new Sprite();
addChild(cubeContainer);

然后,将所有多维数据集实例添加到该容器中:

cubeContainer.addChild(recC01);  //instead of just addChild(recC01)

现在,当你想让它们全部消失时,你可以这样做:

cubeContainer.removeChildren(); //this will remove all children of the container

此方法的唯一缺点是,如果您希望 cubes/shapes 与其他资产分层。使用此方法,您的所有形状都将组合在一起,相当于一层。

2。删除 class

的所有实例

因为看起来你所有的立方体都是同一个 class 的实例,你可以循环显示列表并像这样删除它们:

function clearCubes(e:Event = null):void {   //added the e:Event = null in case you want to attach this function to an event listener
    //loop backwards through all the children (we loop backwards so the index doesn't get messed up when you remove an item)
    var i:int = numChildren;
    while(i--){  //while i is above 0
        if(getChildAt(i) is cube01){  //if this child is a cube01 instance
            removeChildAt(i); //remove it
        }
    }
}

如果你有一大堆 classes(在评论中提到),你可以给他们一个共同的基础 class。使用以下 text/code 创建一个名为 AnimationObject.as 的文件(与您的 .fla 文件位于同一目录中):

package {
    public class AnimationObject extends flash.display.MovieClip {

    }
}

然后,在 Flash/AnimatePro 中将基础 class 添加到您的每个形状。这可以通过转到库中的 symbol/movie 剪辑属性,并将 AnimationObject 作为基础 class(而不是 flash.display.MovieClip)来完成。然后在上面的代码示例中,将 is cube01 替换为 is AnimationObject.

3。在数组

中跟踪它们

虽然我更喜欢容器,但如果您需要以 complex/custom 方式对对象进行分层,则使用数组可为您提供更大的灵活性。

//first create an array variable to hold a reference to all your shapes
var myShapes:Array = [];

//then, whenever you create a new shape/cube, add it to the array
function attachC1() {
   recC01 = new cube01();
   recC01.x = -158;
   recC01.y = 159;
   addChild(recC01);

   myShapes.push(recC01); // <-- THIS LINE added to your function

   trace("cube1");
}

然后,当您希望它们全部消失时,遍历数组并将它们从数组和显示列表中移除:

function clearShapes(e:Event = null):void {   //added the e:Event = null in case you want to attach this function to an event listener
    while(myShapes.length){
        removeChild(myShapes.pop()); //pop will remove the last item in the array and return that item
    }
}