如何删除舞台上所有具有静态形状的孩子?

我正在进行弹丸运动仿真,其中一个选项涉及使用图形函数。

因此,当我按下“图形”按钮(button_2)时,图形模板层可见。有一个预先计算的数组,带有必须在图形上绘制的值的坐标。

对于每个坐标(每0.1秒,如倒数计时器所示),将在其中放置影片剪辑“点”。然后创建一个新的圆形并将其放置在同一点上(复制其坐标)。因此,该阶段现在具有抛物线虚线。但是,按下“后退”按钮时,所有创建的圆圈不会按预期消失/重置(删除所有子项)。

我尝试使用循环功能删除所有子项,但我不断收到错误消息。

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,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);
    }


    }

}

我收到以下错误:

  • 通过静态类型为flash.display:Shape的引用访问可能未定义的属性numChildren。

  • 通过静态类型为flash.display:Shape的引用调用可能未定义的方法removeChildAt。

我希望所有创建的“ circle”变量都将被重置/删除。我该怎么办?

hzh600606 回答:如何删除舞台上所有具有静态形状的孩子?

有几种方法可以使图片结构化。就我个人而言,我会寻求 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);
        }
    }
}
本文链接:https://www.f2er.com/3131648.html

大家都在问