ForumsProgramming Forum(isAlive == false) vs (root == null)

6 10004
weirdlike
offline
weirdlike
1,299 posts
Prince

As you can see by the img below, there are a few class files associated with then main document.

http://i.imgur.com/Bo60LXn.jpg
img link

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

  • 6 Replies
Hectichermit
offline
Hectichermit
1,828 posts
Bard

Or how bout something like this?

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?

weirdlike
offline
weirdlike
1,299 posts
Prince

Or how bout something like this?


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".
Hectichermit
offline
Hectichermit
1,828 posts
Bard

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.

dunno just one idea...

weirdlike
offline
weirdlike
1,299 posts
Prince

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

DEMO

weirdlike
offline
weirdlike
1,299 posts
Prince

Or how bout something like this?


I did come up with a working line

addEventListener(Event.ENTER_FRAME, loop);
function loop(event):void
{
try
{
//loop function
}
catch(Error)
{
removeEventListener(Event.ENTER_FRAME, loop);
//remove other listeners
}
}

while it does work when the error is thrown, if there are no errors it will continue to loop and does not get removed when removeChild occurs
weirdlike
offline
weirdlike
1,299 posts
Prince

bump*

Anyone else have any suggestions on the topic?

Showing 1-6 of 6