Without looking at the code, lets assume that every class has listeners. If you removeChild without removing the listeners you get this annoying error message.
TypeError: Error #1009: Cannot access a property or method of a null object reference.
Two nifty ways of getting rid of this message
addEventListener(Event.ENTER_FRAME, loop); function loop(event):void { if(isAlive == false) { removeEventListener(Event.ENTER_FRAME, loop); //remove other listeners } else { //loop function } }
OR
addEventListener(Event.ENTER_FRAME, loop); function loop(event):void { if(root == null) { removeEventListener(Event.ENTER_FRAME, loop); //remove other listeners } else { //loop function } }
Whats your favorite method?
Is there a different method?
What do you think are the pro's and con's of each method
Also depending on what Object instance the error is looking for from within your code the inheritance might be too abstract for the code to recognize the object you are looking for.
Lets say the Game Play class has a listener for Hero does it communicate to its parents that a Hero Object exists?
I will look into that and see if I can come up with a working line that does the job
Lets say the Game Play class has a listener for Hero does it communicate to its parents that a Hero Object exists?
The game class has a listener for when the hero collides with the ground on the level. When the game class is removed the listener still exists waiting for the hero to collide with the ground, when in fact the hero (as well as the level) was removed when the game class was removed, making it a null object. Hence throwing the error "Cannot access a property or method of a null object".
Well I am assuming that the Hero and level are Extending Sprite or Movieclip?
The Game Play class has no parents? right? its just managing the objects that are on the stage/screen?
its kinda tough to read the whole structure with the image because there is Inheritance and composition in there. Maybe if the chart used a dotted line for composition and a solid line for inheritance, don't know the standard but there is one out there I forget what its called anyways...
Just to clear it out inheritance uses the "extends" keyword in a class while composition create an instance of an object within the class
But anyways what you need is to remove the listeners by creating a class lets say we call it GameObject, GamePlay manages GameObjects it counts all its GameObjects in an array or something similar when an Object is removed the listener is removed for it as well. Seeing how it is collision detection and you might end up with hundreds of GameObjects you will need a way to manage all of them.
Each GameObject can have its own listener for collision inside itself by a function that detects if another GameObject is within its space.
creating an instance of an array of GameObjects and using the removeChild() function should remove the listeners as well.
Well you have it pretty much spot on. Except when the class is removed the listener still exists.
I have created a demo with just the main document class and the game class, the game class traces how many times it loops. Then, when it loops 50 times it removes the class but it still keeps tracing loops past 50