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

I can't add a movieclip to the stage using a function

Participant ,
Feb 26, 2021 Feb 26, 2021

Copy link to clipboard

Copied

Hi.

I am trying to add a movieclip to the stage from the library.

The mc has a linkage name in the library of popup_Tania.

At the end of the code, inside the if statement, I try and call the function with this.addPopuup() 

It doesn't work. 

 

this.stop(); //stop here on first frame

this.s1_b1.visible =false;
this.s1_b2.visible =false;
this.s1_b3.visible =false;
this.s1_b4.visible =false;
this.engagement_container.visible=false;

btn_integer = 0;

this.btn.addEventListener("click", clickHandler.bind(this));

function clickHandler(event) 
{
  btn_integer = btn_integer + 1;
	if (btn_integer == 1) {
		this.s1_b1.visible = true;
		this.engagement_container.visible=true;
	} else if (btn_integer == 2) {
		this.s1_b2.visible=true;
	} else if (btn_integer == 3) {
		this.s1_b3.visible=true;
	} else {
		this.s1_b4.visible=true;
		
		this.addPopup();
	}

}

function addPopup() {
	
	this.s1_b4.visible=false;
	
	var Tania = new lib.popup_Tania();
    Tania.x = 150;
    Tania.y = 250;
    this.addChild(Tania);
}

 

 

Views

172

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

correct answers 1 Correct answer

Community Expert , Feb 26, 2021 Feb 26, 2021

Hi.

 

The this keyword inside of the addPopup function refers to the global window object and not to the main timeline.

 

An easy way to fix the issue is to replace this by the global var exportRoot which is a reference to the main timeline.

exportRoot.addChild(Tania);

 

I hope it helps.

 

Regards,

JC

Votes

Translate

Translate
Community Expert ,
Feb 26, 2021 Feb 26, 2021

Copy link to clipboard

Copied

Hi.

 

The this keyword inside of the addPopup function refers to the global window object and not to the main timeline.

 

An easy way to fix the issue is to replace this by the global var exportRoot which is a reference to the main timeline.

exportRoot.addChild(Tania);

 

I hope it helps.

 

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 ,
Feb 26, 2021 Feb 26, 2021

Copy link to clipboard

Copied

Brilliant - it works BUT I had to paste the code into the if statement because the addPopup function doesn't get called.

How can I call the addPopup function?

 

	} else {
		this.s1_b4.visible=true;
		this.w4.visible = true;
		
		//this.addPopup(); --------- doesn't work. // commented out.
		this.s1_b4.visible=false;
	
	var Tania = new lib.popup_Tania();
    Tania.x = 350;
    Tania.y = 450;
	exportRoot.addChild(Tania);
	}

}
// doesn't get called for some reason.
function addPopup() {
	
	this.s1_b4.visible=false;
	
	var Tania = new lib.popup_Tania();
    Tania.x = 150;
    Tania.y = 250;
	exportRoot.addChild(Tania);
}
	

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 ,
Feb 26, 2021 Feb 26, 2021

Copy link to clipboard

Copied

Oh sorry. I forgot to mention it.

 

It's because addPopup is a function and not a method (meaning it doesn't belong to an object). So to invoke a function, you call it directly without using the this keyword.

 

So instead of:

this.addPopup();

 

You write:

addPopup();

 

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 ,
Feb 26, 2021 Feb 26, 2021

Copy link to clipboard

Copied

LATEST

Thank you so much, You are really helping me to learn and fast.

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