I have a button that loads a movie clip and I want to be able to close that same movieclip by clicking anywhere.
This is my code so far:
btn_2.addEventListener(MouseEvent.CLICK, goBio);
function goBio (e:MouseEvent):void {
var newBio:BioToc = new BioToc();
this.addChild(newBio);
newBio.x = 25;
newBio.y = 45;
}
Since that object is declared inside a function it will be harder to target, so move the declaration outside the function...
var newBio:BioToc;
and just use
newBio = new BioToc();
inside the function.
Then you will be able to directly target the newBio instance.
To remove it, you can add a MOUSE_UP event listener for the stage inside the function and in the event handler function for that new listener just use this.removeChild(newBio);
So how would you assign the listener to the stage?
This is my code now:
btn_2.addEventListener(MouseEvent.CLICK, goBio);
function goBio (e:MouseEvent):void {
var newBio = new BioToc();
this.addChild(newBio);
newBio.x = 25;
newBio.y = 45;
}
stage.addEventListener(MouseEvent.MOUSE_UP, goBio);
function goBio (e:MouseEvent):void {
var newBio = new BioToc();
this.removeChild(newBio);
newBio.x = 25;
newBio.y = 45;
}
You got very close except for a few things...
You now have two functions named goBio, which is not going to sit well with the compiler. Assign a different name for the new function.
Also, what was the only line I said you needed in the function for removing the movieclip?
Lastly, you still haven't done the part you said you understood from my first response. If you don't do it, your second function will not be able to target the movieclip
If I remove var from this line var newBio = new BioToc(); then it stops working all together. So I'm not sure what you mean by just changing it to newBio = new BioToc();
I'm not sure what you mean by changing goBio, can I just put any word in front of Bio to change it?
This is what I did and now I get an error that says "Scene 1, Layer 'actions', Frame 1, Line 10. 1084: Syntax error: expecting leftparen before this.
btn_2.addEventListener(MouseEvent.CLICK, goBio);
function goBio (e:MouseEvent):void {
var newBio = new BioToc();
this.addChild(newBio);
newBio.x = 25;
newBio.y = 45;
}
stage.addEventListener(MouseEvent.MOUSE_UP, removeBio);
function removeBio this.removeChild(newBio);
var newBio = new BioToc();
newBio.x = 25;
newBio.y = 45;
}
There is nothing for me to gain in writing your code for you, but there is for you. If you go back thru everything I have offered and fix your code to what I describe you will not have no errors. As it goes now, you have several and you have not done anything I have indicated you should do except for assigning the stage an event listener correctly.
North America
Europe, Middle East and Africa
Asia Pacific