如何删除舞台上所有 children 个静态形状?

How to remove all children of a static shape on stage?

我正在做弹丸运动模拟,其中一个选项涉及使用图形函数。

因此,当我按下图表按钮 (button_2) 时,图表模板图层可见。有一个 pre-calculated 数组,其中包含必须在图表上绘制的值的坐标。

对于每个坐标(每 0.1 秒,由倒计时器指示),一个 movie-clip 'point' 被放置在那里。然后创建一个新的圆形并将其放置在同一点上(复制其坐标)。因此,舞台现在有一条抛物线虚线。但是,当按下 'back' 按钮时,所有创建的圆圈都没有按预期 disappear/reset(删除所有 children)。

我尝试使用循环函数删除所有 children,但我不断收到错误消息。

button_2.addEventListener(MouseEvent.CLICK, goToGraph);

function goToGraph(event:MouseEvent):void
{

graphTemplate.visible = true;
backToSim1.visible = true;
point.visible = true;

point.x = 42
point.y = 608

var vx = velocity*Math.cos(angle/(180/Math.PI));
var vy = velocity*Math.sin(angle/(180/Math.PI));
var Time = int(((2*vy)/9.81)*100)/100

if (Time != 0) {


    var t :Number = 0;
    var position:Array = new Array();
    var pos_idx :int = 0; //the position within the array


    while(t <= Time)
    {
        position[ pos_idx ] = (vy * t) - 4.905 * (t * t);
        trace("position[" + pos_idx + "]: " + position[ pos_idx ] );

        t += 0.1;
        t = Number( t.toFixed(3) ); 
        trace("t is: " + t);

        pos_idx += 1; 
    }



    var fl_TimerInstance:Timer = new Timer(100, (Time*10));
    fl_TimerInstance.addEventListener(TimerEvent.TIMER, fl_TimerHandler);
    fl_TimerInstance.start();
    var a = 0;
    var timeElapsed = 0;


    function fl_TimerHandler(event:TimerEvent):void
    {
        a = a+1;



        point.x = point.x + (vx*1.2);
        point.y = 608 - (position[a]*10);
        timeElapsed = timeElapsed + 1;

        var circle:Shape = new Shape();
        circle.graphics.clear();
        circle.graphics.lineStyle(2,0x000000);
        circle.graphics.beginFill(0x990000);
        circle.graphics.drawCircle(0,0,1);
        circle.graphics.endFill();
        addChild(circle);
        circle.x = point.x
        circle.y = point.y


        if (position[a+1] == null) {
            point.visible = false;

            }

    }

}

    backToSim1.addEventListener(MouseEvent.CLICK, fl_ClickToHide_2);

    function fl_ClickToHide_2(event:MouseEvent):void
    {
    graphTemplate.visible = false;
    backToSim1.visible = false;
    point.visible = false;

    while (circle.numChildren > 0) {
    circle.removeChildAt(0);
    }


    }

}

我收到以下错误:

我希望所有创建的 'circle' 变量都是 reset/deleted。我该怎么做?

有几种方法可以使图片结构化。就我个人而言,我会选择 Array 解决方案,但是您的代码很乱(格式错误,函数内部的函数),所以求助于名称。

var Circles:Array = new Array;

function fl_TimerHandler(event:TimerEvent):void
{
    // ...

    var circle:Shape = new Shape();

    circle.name = "Circle";

    // ...
}

function fl_ClickToHide_2(event:MouseEvent):void
{
    // ...

    for (var i:int = numChildren - 1; i >= 0; i--)
    {
        var aChild:DisplayObject = getChildAt(i);

        if (aChild.name == "Circle")
        {
            removeChildAt(i);
        }
    }
}