Skip to main content
francescot
Inspiring
February 15, 2021
Question

AddChild removeChild mouse click

  • February 15, 2021
  • 1 reply
  • 336 views

I've been experimenting with addChild removeChild but the respective movie clips don't initiate when called from their assinged button they only begin to funtion if the oposite button is clicked first.

Example click button1 nothing, click button2 nothing, click button1 again and it works.

I suspect it's something to do with the removeChild function, but I'm not at all sure why it's not initiating on first click.

 

this.buttonAddRec.addEventListener("click", addPurpleRect.bind(this));
this.buttonAdd.addEventListener("click", myRectangle.bind(this));

function addPurpleRect()
{
	if (!addPurpleRect)
	{
		addPurpleRect = new lib.RectangleTwo();
		addPurpleRect.x = 250;
		addPurpleRect.y = 150;
		this.addChild(addPurpleRect);
	}
	
	else (myRectangle)
	{
		this.removeChild(myRectangle);
		myRectangle = null;
	}
	
}

function myRectangle(e)
{
	if (!myRectangle) {
		myRectangle = new lib.Rectangle();
		myRectangle.x = 150;
		myRectangle.y = 150;
		this.addChild(myRectangle);
	}
	
	else (addPurpleRect)
	{
		this.removeChild(addPurpleRect);
		addPurpleRect = null;
	}	
}
    This topic has been closed for replies.

    1 reply

    kglad
    Community Expert
    Community Expert
    February 15, 2021

    try:

     

    var addPurple= false;  // probably not needed

    var myRectangle=false;  //  ""

    this.buttonAddRec.addEventListener("click", addPurpleRect.bind(this));
    this.buttonAdd.addEventListener("click", myRectangle.bind(this));

    function addPurpleRect()
    {
    if (!addPurpleRect)
    {
    addPurpleRect = new lib.RectangleTwo();
    addPurpleRect.x = 250;
    addPurpleRect.y = 150;
    this.addChild(addPurpleRect);
    }

    else if(myRectangle){  // without if, this is nonsense
    this.removeChild(myRectangle);
    myRectangle = null;
    }

    }

    function myRectangle(e)
    {
    if (!myRectangle) {
    myRectangle = new lib.Rectangle();
    myRectangle.x = 150;
    myRectangle.y = 150;
    this.addChild(myRectangle);
    }

    else if(addPurpleRect) {  // without if, this is nonsense
    this.removeChild(addPurpleRect);
    addPurpleRect = null;
    }
    }

    francescot
    Inspiring
    February 17, 2021

    Hello

    Thanks for your help.

    This hasn't worked for me, Ive added the "else if" that only brings up one movie clip.

    Also adding the var stops it altogether.

     

    kglad
    Community Expert
    Community Expert
    February 17, 2021

    var addPurple= false;  // probably not needed

     

    should be

     

    var addPurpleRect= false;  // probably not needed

     

     

    yikes, i just notice you used myRectangle as a function name and as a variable.  FIX THAT.