Skip to main content
Akenekiart
Participant
May 23, 2023
Answered

Is it possible to removeChild inside a movieclip that has been placed in Stage from the library? JS

  • May 23, 2023
  • 2 replies
  • 239 views

Hi. The movieclip, that was added to the main stage, is "inputs_1". Is there a possibility inside it, in the function given below, to pass the information to remove it from the main stage? In main stage i called it by this code:

var Inputs1 = new lib.inputs_1();
stage.addChild(Inputs1)


Function im movieclip called: "inputs_1".

function gotoNextFrame(){
	if (pointsInputs == 2 || chanceInputs == 2){
		if(pointsInputs == 2){
			outcome++;
			UpdatePoints();
		}
		exportRoot.mouseEnabled=false;
		let selected = [];
			setTimeout(()=>{
				pointsInputs = 0;
				chanceInputs = 0;
				exportRoot.stage.removeChild(exportRoot.Inputs1)
				//this.removeChild(this);
				//parent.removeChild(inputs_1)
				exportRoot.gotoAndPlay('frame2');
		}, 2000); 
	}
}

 As you can see above, I tried without success.

This topic has been closed for replies.
Correct answer JoãoCésar17023019

Hi.

 

You're storing the newly added instance in a variable. You should store it in a property of the main timeline if you plan to refer to it in another frame or timeline. For example:

// main timeline
this.inputs1 = new lib.inputs_1();
stage.addChild(this.inputs1);

// ...

// child Movie Clip
stage.removeChild(exportRoot.inputs1);

 

Alternatively, you can use the children property of the stage object.

stage.removeChild(stage.children[1]); // the index may be different

 

I hope this helps.

 

Regards,

JC

2 replies

JoãoCésar17023019
Community Expert
JoãoCésar17023019Community ExpertCorrect answer
Community Expert
May 23, 2023

Hi.

 

You're storing the newly added instance in a variable. You should store it in a property of the main timeline if you plan to refer to it in another frame or timeline. For example:

// main timeline
this.inputs1 = new lib.inputs_1();
stage.addChild(this.inputs1);

// ...

// child Movie Clip
stage.removeChild(exportRoot.inputs1);

 

Alternatively, you can use the children property of the stage object.

stage.removeChild(stage.children[1]); // the index may be different

 

I hope this helps.

 

Regards,

JC

Akenekiart
Participant
May 24, 2023

Thanks a lot! The first solution works fine. Could you explain to me how this works? 😄

JoãoCésar17023019
Community Expert
Community Expert
May 24, 2023

You're welcome!

 

The main reason is related to scope. Animate exports the code we write in each frame as a separate method (a function tha belongs to an object). So if you declare a variable inside of a method/function (local variable), there's no way you can access it from outside this same method/function.

 

Code in the 1st frame of the main timeline:

 

Code in the 2nd frame of the main timeline:

 

Part of the code generated by Animate in the output (by default, <your_fla_name>.js :

 

Please let us know if you still have any questions.

 

Regards,

JC

kglad
Community Expert
Community Expert
May 23, 2023

try

 

stage.removeChild(Inputs1)

 

or better:

 

if(Inputs1.stage){

stage.removeChild(Inputs1);

}