Search code examples
actionscript-3removechildaddchild

how delete created children, and restart animation


I have made the following code to create and drag letters, sorry if it is very dirty, but I am just learning AS3, I need you to help me complete the code to delete the created letters and restart the animation, with a button.

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"
}

Solution

  • There are several ways about it. It depends on what you have and can and cannot do there which one to use.

    Way №1. You can have a separate container for all the letters and clean up exactly that container.

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

    Way №2. Enrollment list. You keep a list of things you need to remove.

    // 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;
    }
    

    Way №3. Tagging. If for any impossible reason any of the above doesn't suit you, you can name these letters in a specific way so you can filter them from other objects.

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

    Way №4. Into the weird. If none of above works for you, there's still a way to separate the letters from other objects there.

    // 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;
                }
            }
        }
    }