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;
}
}
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;
}
}
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.
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.
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.
Copy link to clipboard
Copied
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);
}