• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Participant ,
Apr 18, 2023 Apr 18, 2023

Copy link to clipboard

Copied

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?

Views

853

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 18, 2023 Apr 18, 2023

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 18, 2023 Apr 18, 2023

Copy link to clipboard

Copied

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

}

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 18, 2023 Apr 18, 2023

Copy link to clipboard

Copied

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

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 18, 2023 Apr 18, 2023

Copy link to clipboard

Copied

I'm almost through this tunnel. In this case, I only have some generated graphics and text that I'm adding at each of several frames, so the all the addChild objects are Shapes and TextFields. But I'm not familar with all of the structures in your example, so would it apply to children other than MovieClips? For example, using variables:

var myShape: Shape = new Shape();

var myText:TextField = new TextField();

myTxtFmt:TextFormat = new TextFormat();

What would the arguments be to function removeChildF(nameS:String,parent_mc:MovieClip) to clear out any leftover children anywhere in the stage? Since this interface is allowed to bounce around between frames, I'd like to make sure there are no memory leaks or other consquences of abandoned objects.

 

I gather I'd need to do the same with the various EventListeners I add as well. Is there a comparable way to remove EventListeners globally? It seems to me I'd have to remove both everytime a timeout bounced it out of its current frame.

 

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 18, 2023 Apr 18, 2023

Copy link to clipboard

Copied

yes.  it applies to all children, though if it has no name and you don't want to remove all children you'll need to use some unique characteristic to specify it's the child you want to remove.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 18, 2023 Apr 18, 2023

Copy link to clipboard

Copied

So, for example with:

var myShape: Shape = new Shape();

and

addChild(myShape);

in frame2:

 

In frame1, would I call removeChildF(myShape,stage.myShape)?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 18, 2023 Apr 18, 2023

Copy link to clipboard

Copied

I guess the question is when I declare a variable and add an instance of in in a frame, what is its parent? Is it the name of the variable, i.e. myShape in the above example?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 18, 2023 Apr 18, 2023

Copy link to clipboard

Copied

the timeline is the parent of the myShape variable, but the shape object could be parented to any displayobjectcontainer.

 

in your example, the movieclip whose timeline where the variable is defined is also the parent movieclip but at different times.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 18, 2023 Apr 18, 2023

Copy link to clipboard

Copied

anytime after frame 2 code executes, if your code is on the same timeline, you can use:

 

this.removeChild(this.myShape);

 

or

 

removeChild(myShape);

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 19, 2023 Apr 19, 2023

Copy link to clipboard

Copied

So, to clarify, I'm not having a problem removing the child that's added in frame2 while I'm in frame2, My problem is removing the child added in frame2 when I've gone to frame1 by a timeout, rather than through navigation, where I can remove children before moving to another frame.

 

When the variables that are added in frame2 are also declared in frame2, I get an error if I try to remove them in frame1. Are you saying that I should declare all variables in frame1, which are then accessible in frame2, and can be removed in frame1?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 19, 2023 Apr 19, 2023

Copy link to clipboard

Copied

again, the better solution is to stop executing code that you do not want to repeatedly execute (in frame 1).

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 19, 2023 Apr 19, 2023

Copy link to clipboard

Copied

It's not that I'm executing code I don't want to repeatedly execute in frame1, it's that I have some children  left behind in frame2, each time I'm pulled into frame1 by a timeout. I get a delayed runtime error and lockup after a timeout and couple of entry and exits to frame2 when they are left behind.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 19, 2023 Apr 19, 2023

Copy link to clipboard

Copied

LATEST

upload your fla to a file server and post a link, or send to my email.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 18, 2023 Apr 18, 2023

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 18, 2023 Apr 18, 2023

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines