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

AddChild removeChild mouse click

Explorer ,
Feb 15, 2021 Feb 15, 2021

Copy link to clipboard

Copied

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

Views

162

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

Copy link to clipboard

Copied

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

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
Explorer ,
Feb 17, 2021 Feb 17, 2021

Copy link to clipboard

Copied

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.

 

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

Copy link to clipboard

Copied

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.

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

Copy link to clipboard

Copied

Thanks for your help on this.

I'm not sure what you mean as both addPurpleRect and myRectangle are functions and now variables.

Originally they were just functions. What I'm trying to achieve doesnt work with var addPrupleRect and var myRectangle. I've tried a few ways to alter the code but it either doesnt work or only one rectangle mc is placed on stage.

 

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

LATEST

function myRectangle(e)  // myRectangle is a function here
{
if (!myRectangle) {  // myRectangle is a variable here and the lines below
myRectangle = new lib.Rectangle();
myRectangle.x = 150;
myRectangle.y = 150;
this.addChild(myRectangle);
}

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