the as3 code for the frame still runs even after I delete the child it is in...

I'm making a game where after you shoot a bullet, the bullet destroys itself at the end of its timeline.  There is also an event on the main stage that resets the game by deleting all the objects, but if the game gets reset while a bullet is on the self destruct frame, it produces an error, because apparently even after the bullet has been destroyed, the code telling it to destory itself still runs, but it can't destroy itself because it no longer exists!!  How can I fix this!


Below I have created a simplified version of my problem as an example so you can see it in action for yourself.

//This Code is in the first frame of the stage root.      
while (numChildren!=0)
   {
trace(this.numChildren+ " stage children.")
removeChildAt(this.numChildren-1);
      }
      trace(this.numChildren+ " stage children after deleting them all.")
      stop();



------------------------------------------


//On the stage are 4 MovieClip instances of "TestBox",  "TestBox" has this code in the second frame.
MovieClip(parent).removeChild(this);
stop();



WHEN I RUN IT, THIS IS THE OUTPUT.
4 stage children.
3 stage children.
2 stage children.
1 stage children.
0 stage children after deleting them all.
TypeError: Error #1009: Cannot access a property or method of a null object reference.
      at test_fla::TestBox_1/test_fla::frame2()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
      at test_fla::TestBox_1/test_fla::frame2()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
      at test_fla::TestBox_1/test_fla::frame2()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
      at test_fla::TestBox_1/test_fla::frame2()

cakepizzaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Aneesh ChopraCommented:
I recommend to attach your source files for review, this will help to provide the quicker solution
0
cakepizzaAuthor Commented:
ok, I have attached a small  fla file that basically is just the example code I posted above.  It would not let me attach an fla file though, even in a zip folder, so I changed the file extension to .bmp.  Just rename it back to .fla and check it out.

  The goal is to get the first frame to REALLY delete the 4 boxes, so that their action script (which references themselves) will not run, because when the action script in the boxes run, it causes an error, since they are referencing themselves, which are supposed to no longer exist.
test.bmp
0
Aneesh ChopraCommented:
there are many forum/blog mentioned similar issue without any sureshot solution..
hence,
here is the updated code which is actually workaround for your issue:


var total = this.numChildren;
for (var i=total-1; i>=0; i--) {
	trace(this.numChildren+ " stage children.");
	var mc = getChildAt(i);
	mc.stop();
	removeChild(mc);
	mc = null;
}
//
trace(this.numChildren+ " stage children after deleting them all.");
stop();

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
blue-genieCommented:
hello.
just wondering -  the fact that on the maintimeline you're removing all the children, then in frame 2 of the children, you're saying remove this, but they're removed so you get a null reference?
0
cakepizzaAuthor Commented:
yes.  The action script for the second frame of the children should not be running, because they are children that have been removed already.  Looking at the solution, it seems that simply removing children is not enough to actually get rid of them.  You also have to stop their timeline and set them to null manually.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Adobe Flash

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.