Copy link to clipboard
Copied
If I use
stage.removeChild (myIdentifier)
myIdentifier = null;
will that also remove any eventListeners attached to that asset?
I might have eventListeners attached through the main.as class or through the actual asset's class itself.
When I remove that asset would I go through and remove each listener 1 by 1 in the same way they were added?
stage.removeEventListener(event.Event, eventIdentifier);
myIdentifier.removeEventListener(event.Event, eventIdentifier); <----- will this work if the event Listener is added in the Class.as and not in the Main.as class im coding in?
Sorry if anything is confusing, im pretty new to all of this
In practice, any class that contains an addEventListener() method call on a target should have a removeEventListener() call in the same class. Calling removeEventListener() in another class file would require that other class having a reference to both the target of the event listener (myIdentifier) and the callback function (eventIdentifier), otherwise it would throw an error.
When removing event listeners, just make sure that happens before you call "myIdentifier = null;".
Simply removing a disp
...Copy link to clipboard
Copied
If a display object is removed from the display list, setting its reference to null after removing it does not ensure that the object is frozen. If the garbage collector doesn’t run, the object continues to consume memory and CPU processing, even though the object is no longer displayed. To make sure that the object consumes the least CPU processing possible, make sure that you completely freeze it(remove all listeners from it) when removing it from the display list.
You can do it from any class witch can access the display object(myIdentifier) and listeners(eventIdentifier) or the reference of them.
Copy link to clipboard
Copied
In practice, any class that contains an addEventListener() method call on a target should have a removeEventListener() call in the same class. Calling removeEventListener() in another class file would require that other class having a reference to both the target of the event listener (myIdentifier) and the callback function (eventIdentifier), otherwise it would throw an error.
When removing event listeners, just make sure that happens before you call "myIdentifier = null;".
Simply removing a display object from a display stack does not remove it's event listeners and could prevent the object from being garbage collected because the event listeners would potentially be keeping the object alive. According to the AS3 documentation‌, if you set the useWeakReference parameter to true it would allow it to be garbage collected, but it is still best practice to remove all event listeners you add.
I usually like to have a shutdown() method (or destroy() or dispose() ) that cleans up an object I am wanting to full get rid of that handles all of the removing of event listeners and setting variables to null values.
Copy link to clipboard
Copied
Thank you very much!
I have never thought of something like a cleanUp(); method, would this be an example of how to go about doing it? (this is just off the top of my head, please forgive any errors)
public function PlayerTurret ():void {
stage. addEventListener(Event.ENTER_FRAME, frameListener);
Player.Turret.addEventListener(MouseEvent.CLICK, fireBullet);
}
public function PlayerTurretCleanUp():void {
Player.Turret.removeEventtListener(MouseEvent.CLICK, fireBullet);
stage.removeChild(Player);
Player = null;
}
public function frameListener (event:Event):void{
if (Player.Turret.currentFrame == 30) {
PlayerTurretCleanUp();
}
*EDIT*
I cant mark 2 answers as correct?? That sucks, I very much appreciate both of your answers.
What is this garbage collector you speak of?
Copy link to clipboard
Copied
Since your "frameListener()" method isnt being removed in the code you provided, I would recommend changing the condition to:
if (Player != null && Player.Turret.currentFrame == 30) {
PlayerTurretCleanUp();
}
You should add a check to make sure that the Player object is not null otherwise, you could have an error in the code after the Player object became null and the event listener attempts to run another check against Player.Turret.currentFrame.
As to your question about the garbage collector, that is something that is built into the Flash Player, the AIR runtime, and other programming runtimes like Java. Whenever you create a variable and give it a value (number, string, MovieClip, NetStream, etc) it takes up a specific amount of RAM. Other things like event listeners take up RAM as well. Once you are done with an object and set the variable to null, that does not actually free up the RAM space it was taking up. It is marking it as available for the Flash Player garbage collector to clean up any assets that are no longer needing to be held in memory and to make that RAM available for other things in the Flash Player or other applications and services on the computer.
Now if you are going to be creating and destroying objects on a frequent basis that get used often, you should do a (Google/Yahoo/Bing) search for "as3 object pool" and find several examples, both simple and complex, on how to keep objects you create and store them for later use. The idea behind this is that creating and destroying objects frequently can make your app or SWF start to hang every now and then. That freezing up is most likely the garbage collector working and it is actually taking up resources freeing up and then reallocating RAM. Some of this stuff I may have over simplified and if any of the other frequenters of these forums wants to correct or further explain what Im taking about, they can, but it seemed like you might be somewhat new at this and I didnt want to overwhelm you.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now