0

I've been debugging the following issue for quite awhile now and have hit a wall.

I've set up a project in Flash (CS4, btw) that has a set of keyframes that I move between to represent the various screens of a game. One of them has a MovieClip defined (with children inside it) representing an option menu, that appears on a couple of different keyframes.

The problem I'm having is that this MovieClip reference seems to be accessible when I first enter the keyframe (using "gotoAndStop"), and occassionally when I move to other frames and back. But in at least one case, when I exit the frame and come back, I get a null reference error (TypeError: Error #1009: Cannot access a property or method of a null object reference). when I try and access it (getChildByName("optionMenuTitle")). I've even tried having the system iterate from 0 to numChildren and print out the name of each object, but it returns NULL at position 7 despite returning numChildren as 9. Does anyone have any idea why this particular MovieClip reference is NULL only in this case??

Here is a basic (abbreviated) rundown of the process occurring:

//set up function to be fired on frame construction
addEventListener(Event.FRAME_CONSTRUCTED, fadeIn, false, 0, true);  

public function fadeIn(event:Event):void {
   _handler.handle(); //this function is called which runs the debug statement below
   trace (mainDoc.numChildren); //displays 9
   for (var i = 0; i < mainDoc.numChildren; i++) { trace(mainDoc.getChildAt(i).name); } //throws null when it gets to 7 
   optionMenuTitle = OptionMenu(mainDoc.getChildByName("optionMenuTitle")); //the original failed call that caused me to debug
}

edit: One other potentially useful bit of information. If I comment out the getChild commands above that error, the frame loads and I can see the MovieClip visually displayed on the stage (although it's not interactive and is constantly cycling through the frames of its child objects). Still can't access it programatically though.

another edit: The object in question is a subclass of MovieClip that I named "OptionMenu". I put a breakpoint in the OptionMenu constructor, and when the frame loads correctly, that breakpoint is hit. When I get the error above, the breakpoint in the constructor is never hit. The debugger doesn't seem to give me access to see what's going on inside Flash's mind when it's instantiating the frame, however, so I can't see the logic as to why the constructor is never called.

roberttdev
  • 42,762
  • 2
  • 20
  • 23
  • Show how/where you're adding `optionMenuTitle` to `mainDoc` – Sam DeHaan May 14 '12 at 19:25
  • It is defined in the Flash GUI. On that keyframe I dropped a visual representation of the object out of the library to the correct spot on the document. It is not added via ActionScript. – roberttdev May 14 '12 at 19:29
  • It is the `name` that evaluates to `null`, not the MovieClip. If it were the clip itself, a null reference **error** would be thrown. So you probably just haven't set the `name` property correctly. – weltraumpirat May 14 '12 at 20:40
  • It is indeed a null object reference (which is why accessing by getChildAt fails as well, I assume). I'll update to clarify. – roberttdev May 14 '12 at 21:22
  • Unfortunately, I was originally using "enterFrame" when I saw the error. I changed it to "frameConstructed" in the hopes that it was an issue with the object not being initialized yet at "enterFrame".. unfortunately it appears the issue is that it is never initialized at all, whether it's enter, constructed, or exit. – roberttdev May 14 '12 at 22:25
  • Any reference returned by `getChildAt(index)`can never be null, unless the index is greater than or equal to the number of children. In other words: If `getChildAt(7)` throws an error, then there are no more than 7 children (at index 0 to 6). Period. Now if you are jumping back and forth between frames, when sometimes the clip is present, and sometimes it is not, there may be errors: An instance is automatically added to the stage only in the keyframe that it first appears in the timeline (not in any frame after that!), and it is removed when there are no further references. – weltraumpirat May 15 '12 at 00:33
  • Hmm.. then why do you think the loop that uses "numChildren" as a stopping condition still tries to access an index of the children array that returns a null reference? And when you say the instance is automatically added only in the first keyframe, should it reappear when I am using goToAndStop to access that keyframe specifically? – roberttdev May 15 '12 at 01:51
  • This is interesting - any chance you can zip it up somewhere so we can get at it - id love to solve this one – Neil May 15 '12 at 08:34
  • OK, I dropped a debug version of the SWF at: http://www.mediafire.com/?mtsy6ivt34yct5k – roberttdev May 15 '12 at 13:18
  • To see the issue, click "Go", click "New Game" and "Standard", click "Skip", then click "Main Menu" on the level select screen. You can also see that everything works fine if you go to "Credits" and then back to "Main Menu". – roberttdev May 15 '12 at 13:29
  • I wanted to see the fla and and code associated with it not the swf. – Neil May 15 '12 at 15:29
  • Codebase pushed to https://github.com/roberttdev/thecopjazzhour – roberttdev May 15 '12 at 16:31
  • If you're not into Git, I also added it as a ZIP at http://www.mediafire.com/download.php?r136jni4cf44805 – roberttdev May 16 '12 at 00:45

1 Answers1

1

Well this one has been driving me crazy. I could not workout why it does not reference your optionMenuTitle when you go back to the frame called title a second time.

The only way I could work around it was to take the 3 buttons out of the OptionMenu MovieClip and put them on the stage with the grey background underneath, essentially doing away with OptionsMenu.

So I moved all initialization code from OptionMenu to your TitleHanlder and also added the destroy calls to your destroy method in TitleHandler for each of the 3 buttons.

I also changed the refs from root to mainDoc:

sound.initialize(LogicGameMain(mainDoc).soundOn);
music.initialize(LogicGameMain(mainDoc).musicOn);

This worked for me as you can still interact with the buttons the second time around. It definately seems like there is some bug with these buttons being nested.

I hope this is useful for you.

Neil
  • 7,861
  • 4
  • 53
  • 74
  • Thanks for all the effort! I was hoping to keep it all in a single reproducible MovieClip so I could re-use it in another screen, but after banging my head on it for days and reading your response, it seems the best use of my time is just to work around it. Flash certainly seems to have some idiosyncracies.. – roberttdev May 17 '12 at 12:32
  • @roberttdev - Totally agree with you, I thought idiosyncracies such as these were left back in AS2, good luck. – Neil May 17 '12 at 12:35
  • For anyone who runs across this, what I ended up doing was taking the MovieClip off the frame in the Flash IDE and adding/removing it in the code instead, which seemed to work. Best I can figure, it's just a fluke in the Flash code. – roberttdev May 22 '12 at 17:54