Differences between code in the timeline and using a Document Class
I had some code directly in the timeline that accessed/modified properties of some movie clips. I then did the proper thing and moved the code to a Document class. I put the code that was formerly in the timeline into the document class constructor.
The issue I'm having is that if I access the movie clip properties "too soon" it reports 开发者_如何转开发that they are null. If I put the code in a timer event callback for half a second or so, or put an addFrameScript call with a frame of around 8 or higher, the movie clip references are valid and it all works.
So for some reason by the time the code in the timeline gets called all of the movie clip instances are valid. However, when inside of a document class constructor, they are not. I've tried using the ENTER_FRAME event callback but the movieclips still aren't ready.
The code is supposed to change some movie clip properties based on some loaderinfo parameters, so I do kind of need to set those properties before anything starts animating.
So for some reason by the time the code in the timeline gets called all of the movie clip instances are valid. However, when inside of a document class constructor, they are not.
That's right. The constructor is called before the main movieclip ("the Timeline") is added to the stage. The timeline is an object that inherits flash.display.MovieClip
, and it has to be instantiated before it can appear anywhere. This also happens when you don't assign a document class, by the way.
To make sure you access properties that have been properly initialized (such as objects you have placed on the stage in the IDE), move the related code into an event handler and listen for Event.ADDED_TO_STAGE.
精彩评论