I have loaded a library's MovieClip
onto the stage with this code:
addChildAt(MC_1, 0);
In this MovieClip
I have some MovieClips
used as buttons. When I press on one of this button it changes color (goto frame2).
After that, I removed MC_1
from stage like so:
removeChildAt(0);
and loaded another MovieClip
similar to the MC_1
like so:
addChildAt(MC_2, 0);
The problem is that, if I load another MC_1
, the last button pressed still remains colored.
How do I unload it completely from the memory?
Apparently you are not loading another MC_1
, you are adding the same instance that's referenced by MC_1
. "Loading" a movie clip is not just adding, it's also instantiating , to do this, you call MC_1 = new Something();
. So, in order to add a completely new movie clip that has a prototype in the library, you have to re-instantiate the variable you use by placing MC_1 = new Something()
where Something
is the name of the movie clip in the library.
You can also make a different approach: you give the prototype a function that'll react on the Event.REMOVED_FROM_STAGE
event, which will make all the buttons in the MC to change their states to default. Like this:
public class Something extends MovieClip {
// any other functionality is here
public function Something() {
// ...
addEventListener(Event.REMOVED_FROM_STAGE,resetMC);
}
private function resetMC(e:Event):void {
button_1.gotoAndStop(1); // make sure to place proper name of buttons
... // place one gotoAndStop() call per button you want to switch back
}
}
Then, as soon as you do removeChild(MC_1)
, this code kicks in, making all of the MC_1
's buttons (well, all that are referenced in the resetMC()
function) to show the corresponding frame (here, button_1
will show first frame). This approach is generally better, because you don't make a new instance to appear in memory, thus you retain the control of the present MC_1
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.