Skip to main content
Inspiring
February 26, 2021
Answered

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

  • February 26, 2021
  • 1 reply
  • 433 views

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

 

 

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

    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

    1 reply

    JoãoCésar17023019
    Community Expert
    JoãoCésar17023019Community ExpertCorrect answer
    Community Expert
    February 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

    Inspiring
    February 26, 2021

    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);
    }
    	
    JoãoCésar17023019
    Community Expert
    Community Expert
    February 26, 2021

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