【问题标题】:How to remove all children of a static shape on stage?如何在舞台上删除所有静态形状的孩子?
【发布时间】:2020-03-06 00:43:33
【问题描述】:

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

所以当我按下图表按钮 (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,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 对象中删除所有 circle,而不是子对象。
  • 这是有道理的。那么,我怎样才能将它们全部删除?就像访问这些圈子并删除它们一样。

标签: arrays loops actionscript-3 flash actionscript


【解决方案1】:

有几种方法可以构造图片。就我个人而言,我会选择 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);
        }
    }
}

【讨论】:

  • 成功了!太感谢了!以后我会尝试更多地组织我的代码,所以感谢您的反馈。
猜你喜欢
  • 2011-08-03
  • 1970-01-01
  • 2013-09-14
  • 1970-01-01
  • 2015-04-09
  • 2019-04-18
  • 2012-05-13
  • 1970-01-01
  • 2012-08-13
相关资源
最近更新 更多