Skip to main content
Inspiring
February 27, 2021
Answered

removeChild movieclip not working

  • February 27, 2021
  • 1 reply
  • 1175 views

Hi.

I used this code to add a moveclip dymanically.

var Tania = new lib.popup_Tania();
Tania.x = 150;
Tania.y = 150;
exportRoot.addChild(Tania);

 

I try to remove the mc with the following code with the creatjs.Ticker.on block of code but it doesn't work.

function nextFunction() {

	//exportRoot.removeChild(Tania);
	//removeChild(exportRoot.Tania);
	createjs.Ticker.on("tick", function(e)
{
    exportRoot.removeChild(exportRoot.Tania);
});

	this.btn.removeEventListener("click", clickHandler);
	this.nextButton.removeEventListener("click", nextHandler);
	this.gotoAndPlay("ex2");
}

 

    This topic has been closed for replies.
    Correct answer kglad

    1. that's not what i suggested.  and

    2. you're putting the code in a function where you were warned you need to assure "this" is properly defined (which you probably also failed to do).

     

    anyway, with the code all in one frame you don't have to contend with scope issues and "this" and can simply use:

     



    var Tania = new lib.popup_Tania();

    function addPopup() {
    Tania.x = 150;
    Tania.y = 150;
    exportRoot.addChild(Tania);

    }

    function nextFunction() {

    exportRoot.removeChild(Tania);  // you should have some error checking to assure addPopup() was called
    this.gotoAndPlay("ex2");   // oops, you're using "this" here so you do need to pass "this" when calling nextFunction by appending bind(this) to your function reference where "this" is defined.  ie, nextFunction.bind(this)
    }

    1 reply

    kglad
    Community Expert
    Community Expert
    February 27, 2021

    if the removeChild code is on a different frame than the addChild code, use:

     

    // to add

    this.Tania = new lib.popup_Tania();
    this.Tania.x = 150;
    this.Tania.y = 150;
    exportRoot.addChild(this.Tania);

     

    // to remove

    exportRoot.removeChild(this.tania);  // if this code is in a function, make sure "this" is properly defined

    Inspiring
    February 27, 2021

    It's all on the same frame.

    These are the two functions. I put the "this" keyword in.

    When I called nextFuntion() from my code it does work as it moves to the next frame.

    Still doesn't work.

     

    function addPopup() {
    	
    	
    	var Tania = new lib.popup_Tania();
        this.Tania.x = 150;
        this.Tania.y = 150;
    	exportRoot.addChild(this.Tania);
    
    }
    
    function nextFunction() {
    
    	exportRoot.removeChild(this.Tania);
    	this.gotoAndPlay("ex2");
    }
    	

     

    kglad
    Community Expert
    kgladCommunity ExpertCorrect answer
    Community Expert
    February 27, 2021

    1. that's not what i suggested.  and

    2. you're putting the code in a function where you were warned you need to assure "this" is properly defined (which you probably also failed to do).

     

    anyway, with the code all in one frame you don't have to contend with scope issues and "this" and can simply use:

     



    var Tania = new lib.popup_Tania();

    function addPopup() {
    Tania.x = 150;
    Tania.y = 150;
    exportRoot.addChild(Tania);

    }

    function nextFunction() {

    exportRoot.removeChild(Tania);  // you should have some error checking to assure addPopup() was called
    this.gotoAndPlay("ex2");   // oops, you're using "this" here so you do need to pass "this" when calling nextFunction by appending bind(this) to your function reference where "this" is defined.  ie, nextFunction.bind(this)
    }