Solved

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

Posted on 2008-10-27
5
618 Views
Last Modified: 2012-08-14
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()

0
Comment
Question by:cakepizza
  • 2
  • 2
5 Comments
 
LVL 34

Expert Comment

by:Aneesh Chopra
ID: 22828669
I recommend to attach your source files for review, this will help to provide the quicker solution
0
 

Author Comment

by:cakepizza
ID: 22832916
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
 
LVL 34

Accepted Solution

by:
Aneesh Chopra earned 500 total points
ID: 22833893
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
 
LVL 39

Expert Comment

by:blue-genie
ID: 22842254
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
 

Author Comment

by:cakepizza
ID: 22849330
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How do I change text in a Flash (FLA, SWF) file? 3 1,034
How do I convert a CS4 FLA file to CS6? 1 877
Stop Ads in Browser Google Chrome 3 547
Flash player plugin 13 7 339
I come across a lot of question about how to access things in the document class from a movieclip, or accessing something from a movieclip in the document class. It took me a while to figure this out but once I did it makes life so much easier. …
Sometimes you know that one object has a specific child in it, but you can't find the child. This happened to me when I was trying to code some actionScript to make a toolbar work with its embedded buttons.  My partner had created the toolbar usi…
In this tutorial viewers will learn how to create a basic motion tween animation in Flash Open a new document in Flash: Draw/import an image: Press CTRL + F8 to convert it into a graphic symbol: Select a frame (how long you want the tween to last): …
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question