【问题标题】:how delete created children, and restart animation如何删除创建的孩子,并重新启动动画
【发布时间】:2020-09-02 20:25:09
【问题描述】:

我已经制作了以下代码来创建和拖动字母,如果很脏,很抱歉,但我只是学习AS3,需要你帮我完成删除创建的字母并重新启动动画的代码,用一个按钮。

LetraA.addEventListener(MouseEvent.MOUSE_DOWN, arrastrarA);
function arrastrarA(event: MouseEvent): void {
    var LetraA_1 = new letraA;
    addChild(LetraA_1);
    LetraA_1.x = 72.15;
    LetraA_1.y = 316.45;
    LetraA_1.startDrag();
}

LetraB.addEventListener(MouseEvent.MOUSE_DOWN, arrastrarB);
function arrastrarB(event: MouseEvent): void {
    var LetraB_1 = new letraB;
    addChild(LetraB_1);
    LetraB_1.x = 170.35;
    LetraB_1.y = 316.45;
    LetraB_1.startDrag();

stage.addEventListener(MouseEvent.MOUSE_UP, soltarletras);
function soltarletras(event: MouseEvent): void {
    LetraA.stopDrag();
}

Resetear.addEventListener(MouseEvent.CLICK, eliminarhijos);

function eliminarhijos(event:MouseEvent):void
{
    "I need help here please"
}

【问题讨论】:

    标签: actionscript-3 removechild addchild


    【解决方案1】:

    有几种方法。这取决于你有什么,可以和不能在那里使用哪一个。

    方式№1。您可以为所有字母使用单独的容器并准确清理该容器。

    // Before your code.
    var TheABC:Sprite = new Sprite;
    addChild(TheABC);
    
    // Inside the letter creation methods.
    // I do hope you do not have 26 separate functions for that.
    
        // ...
        TheABC.addChild(LetraA_1);
        // ...
    
        // ...
        TheABC.addChild(LetraB_1);
        // ...
    
    // So, the finale is very simple.
    function eliminarhijos(e:MouseEvent):void
    {
        // Remove all the children at once.
        TheABC.removeChildren();
    
        // If, by any chance, you have a VERY old Flash
        // below Player 11, the one above won't work so
        // you would have to resolve to the one below.
    
        // Proceed while there are children at all.
        // while (ThABC.numChildren)
        // {
        //     // Remove the firstmost child.
        //     TheABC.removeChildAt(0);
        // }
    }
    

    方式№2。报名名单。您保留一份需要删除的内容的列表。

    // Before your code.
    var ABClist:Array = new Array;
    
    // Inside the letter creation methods.
    
        // ...
        addChild(LetraA_1);
        ABClist.push(LetraA_1);
        // ...
    
        // ...
        addChild(LetraB_1);
        ABClist.push(LetraB_1);
        // ...
    
    // Finally.
    function eliminarhijos(e:MouseEvent):void
    {
        // Iterate over the listed letters.
        for each (var aLetter:DisplayObject in ABClist)
        {
            // Removes each of the listed letters, one by one.
            removeChild(aLetter);
        }
    
        // Clear the list.
        ABClist.length = 0;
    }
    

    方式№3。标记。如果出于任何不可能的原因,上述任何一项都不适合您,您可以以特定方式命名这些字母,以便将它们从其他对象中过滤掉。

    // Inside the letter creation methods.
    
        // ...
        addChild(LetraA_1);
        LetraA_1.name = "LETTER";
        // ...
    
        // ...
        addChild(LetraB_1);
        LetraB_1.name = "LETTER";
        // ...
    
    // Finally.
    function eliminarhijos(e:MouseEvent):void
    {
        // Iterate over all children. Backward loop, because if you
        // remove something, the whole body of children shifts down.
        for (var i:int = numChildren - 1; i >= 0; i--)
        {
            var aChild:DisplayObject = getChildAt(i);
    
            // So you have a criteria to figure out if it is a letter or not.
            if (aChild.name == "LETTER")
            {
                removeChild(aChild);
            }
        }
    }
    

    方式№4。进入诡异。如果以上都不适合你,还有一种方法可以将字母与那里的其他对象分开。

    // You need to list all the classes of your letters here.
    var LetterBox:Array = [letraA, letraB];
    
    // The clean up.
    function eliminarhijos(e:MouseEvent):void
    {
        // Iterate over all children. Backward loop, because if you
        // remove something, the whole body of children shifts down.
        for (var i:int = numChildren - 1; i >= 0; i--)
        {
            var aChild:DisplayObject = getChildAt(i);
    
            // Now iterate over all possible letter classes
            // to figure out if the given child belongs to any of them.
            for each (var aClass:Class in LetterBox)
            {
                // Match criteria.
                if (aChild is aClass)
                {
                    removeChild(aChild);
                    break;
                }
            }
        }
    }
    

    【讨论】:

    • 一百万谢谢,前三个选项给了我错误 1120,因为孩子在编译时不存在,他们是在使用应用程序时创建的,再次一百万谢谢。第四个选项是完美的。
    • @SmartSoftware 你可能没有理解我的意图,在№1..№3 中的某些行应该放入函数中,而不是按原样复制粘贴。
    猜你喜欢
    • 1970-01-01
    • 2018-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-11
    相关资源
    最近更新 更多