Saturday, November 10, 2012

The MainTimeline and The Document class

 
In flash, Document Class represents the MainTimeline.
It means, the Variables or MovieClips or Functions defined on Timeline are also accessible in Document Class or vice versa.
And it also simply means, you cannot re-declare the Variables or MovieClips or Functions in Document Class, once you declare the same on Timeline. If you do so, you will get the error "1151: A conflict exists with definition [name] in namespace internal."

Let’s see how Timeline works:
The initialization of a SWF starts with the Document Class constructor function. The code written in constructor function of Document Class gets executed first, after that code written on Timeline frame1 will get executed.
If you don’t use Document Class to write code then flash internally create the one at the time of compiling swf and code written in each frame will get converted to frame number function in Document Class. Ex. If you write code on frame number 1, flash will create the function named frame0 and put all code within this function. This is the flow of execution for all frames and the code on particular function get executed when play head reaches to that frame. Remember one thing here, though Timeline starts with frame1, flash considers this as index 0. so frame1 code will be declared in function named "frame0" and so on.

The Variables and Functions declared on Timeline are accessible throughout the fla and Document Class, as they are considered as internal access specifires. MovieClips, Buttons declared on Timeline have limited scope in particular function, as they exists till the Playhead is present on that frame on timeline. Document Class is always considered and declared as public to access the things in all project.

If you are not using Document Class then also this execution is same for Document Class created by flash at compile time and in constructor function of it using addFrameScript method, which is undocumented feature of flash.

Since frame0, frame1 etc. are the function names that flash uses internally, we cannot use the same named functions. In such situation flash gives error: "1023: Incompatible override."/"1021: Duplicate function definition."
However you can use the same function name except in the same frame. ie. function frame10() will give above arror if it is written on frame10, but you can write the function frame10() on any other frames.
For Document Class you have to extends class as MovieClip or Sprite. but if you have a code on timeline you have to extend the class as MovieClip, extending sprite will give you error as Sprite does not have timeline. 
For more details, create your own swf file having code on timeline and decompile it using Sothink SWF decompiler. In ‘actions’ folder, in ‘resources’ panel you will get the flash compiled code.

Also to clear the difference between MainTimeline, Root and Stage check the following outputs on Timeline and Document Class sepretaly.

trace("this " + this);
trace("root " + root);
trace("root.parent " + root.parent);
trace("stage " + stage);
trace("parent " + parent);