Skip to main content
Inspiring
April 18, 2023
Question

How can children be removed when on a different frame than where they were created?

  • April 18, 2023
  • 2 replies
  • 1423 views

When children are added inside of one frame, say frame3, and a timeout defined in frame1 jumps back to frame1, is there a way to remove the children that were active on frame3 when the timeout occured. I'm not aware of a way to trap the timeout inside frame3, and remove them there when it occurs.

 

Is it a viable practice to declare all variable that will be added as children to any frame as global on frame1?

    This topic has been closed for replies.

    2 replies

    kglad
    Community Expert
    Community Expert
    April 18, 2023

    it doesn't matter when/where a child is added. it can always be removed after it's created.

    hclmed0Author
    Inspiring
    April 18, 2023

    Ok, since it seems to be out of scope if accessed directly from frame1, is that only if it's declared on frame1, or is there another way to make it global? Is that done by declaring it as public? Or can it be reached through the stage, as with your removeChildF function?

     

    Having a timeout is vital, but when it's what initiates the navigation back to frame1, there could be a number of components, listeners and children that need to be removed from the stage.

     

    Also, when an object is added as a child multiple times before removal, is it always overwriting the previous instance, or does it need to be removed as many times as it was added?

    JoãoCésar17023019
    Community Expert
    Community Expert
    April 18, 2023

    Hi.

     

    You should remove the children before calling gotoAndStop and gotoAndPlay. So whenever the time is out, call removeChild and then navigate to another frame.

     

    And if you add children dynamically, you can store them in variables declared in frame 1 or in the parent or in the root. In this way you could you have access to them in all frames of the same timeline.

     

    Please let us know if you have any further questions.

     

    Regards,

    JC

    hclmed0Author
    Inspiring
    April 18, 2023

    The circumstance is that I'm not making an explicit call to frame1. The times that I am, (back button for example), I do remove the children in that function. The problem is that there's a timeout function, (thanks to kglad), on frame1 that listens for mouse movement and switches to frame1 when it's idle for a set period.

     

    If it's not ineffecient to store all of the variables in frame1, I could do it that way, and then just test all the variables for non-null children and remove those that aren't null.

     

    Or is it possible to reach a child anywhere by addressing it through the stage, for example, if var pieChart:Shape = new Shape() is declared and added as a child on frame2, can frame1 access it as pieChart.parent.removeChild(pieChart), as shown in kglad's removeChildF function?

     

    function removeChildF(obj:DisplayObject):void{

    if(obj.stage){

    obj.parent.removeChild(obj);

    }

    }

    kglad
    Community Expert
    Community Expert
    April 19, 2023

    you can always find children if you know the parent:

     

    function removeChildF(nameS:String,parent_mc:MovieClip):void{

    if(nameS){

    for(var i:int=0;i>parent_mc.numChildren;i++){

    if(parent_mc.getChildAt(i).name==nameS){

    parent_mc.removeChildAt(i);

    break;

    }

    } else {

    // remove all children

    for(var i:parent_mc.numChildren-1;i>=0;i--){

    parent_mc.removeChildAt(i);

    }

    }

    }

     

    and re-added children only have a depth change.  ie, an addition child is not added to the parent.

     

     

    however, you should consider just adding the few lines of code needed to prevent re-execution of your frame 1 code. eg, 

     

    var alreadyDefined;

    if(!alreadyDefined){

    alreadyDefined=true;

    // all the code you want to execute exactly once

    }