Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
352 views
in Technique[技术] by (71.8m points)

actionscript 3 - What is the difference between object main timeline, object Stage and root in as3?

I want to know the difference between [object main timeline], [object Stage] and root in as3?

I have read from the topic How stage, root, and MainTimeline Fit Together. But I didn't get clearly.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think the article you linked to sums it up quite nicely (even if it doesn't explain it all that well):

To summarize: one stage, one root per SWF (which is the main timeline) and that root is an instance of a document class or the MainTimeline class if a document class isn't provided

Stage is probably the easiest to understand. There is one stage per Flash Player - think of it as the window that the Flash movie plays in. It is the top-most display object - anything that appears on the screen is a child of the stage. The stage is always the same instance and any reference to stage returns the same value.

Root is the a logical "top" of the display hierarchy for a specific SWF file. As explained in the article, every SWF will have it's own root, which refers to the instance of the document class for that SWF.

You can see the different between root and stage when loading one SWF into another at run-time. Both SWFs will have a different root but the same stage. The root in each SWF will refer to the top-most display object of their own SWF which is their document class.

MainTimeline is the default class used for the document class. The document class is the display object which is added to the stage when the SWF movie is loaded. Underneath it is a normal class which extends MovieClip.

The document class is the MovieClip you see in the Flash editor. This it is where timeline code is kept and where timeline animations are added. The document class can be over-ridden with a custom class. Changing the document class will change the name of the object that root refers to.

Just as I am referred to as "Human", the timeline is referred to by default as "MainTimeline". If my atoms were to be mangled in a tele-porter and I was changed to a different type of thing such as "FreakOfNature", this would be similar to changing the document to a different class - the result is that the timeline would become a different type of thing.

Here's a test you can do to illustrate how the stage, root and document relate:

1.Create an empty FLA file, and add the following timeline code:

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

2.Run the FLA and take note of the output. Note that the timeline code is in a class that extends movie clip of name "MainTimeline". This is the same instance referred to by root. The parent of the MainTimeline instance is Stage.

this [object MainTimeline]
root [object MainTimeline]
root.parent [object Stage]
stage [object Stage]
parent [object Stage]

3.Set the document class to your own class (eg: "Test"). Note that the class doesn't actually have to exist - Flash will prompt you to create it automatically.

enter image description here

4.Run the FLA and note the new output. Note that the timeline code is now in a different class that extends movie clip called "Test". "Test" is the type of the movie clip which is added to the stage.

this [object Test]
root [object Test]
root.parent [object Stage]
stage [object Stage]
parent [object Stage]

From this we can see that Flash uses a default class named MainTimeline for the document class, unless over-ridden with your own. An instance of the document class (be it MainTimeline or your own class) is added to the stage when the SWF is loaded.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...