Tutorials in The ActionScript 3.0 series (3): What does Document Class bring to us?

Source: Internet
Author: User

Document Class ". As the name suggests, it is the class associated with the document.
What is the document? Is the Fla file to be bound to this class.

What's the purpose??
The fundamental purpose of this plug-in is to completely remove the AS code from the Flash design.
From then on, Fla is designed, and all logic code is managed by external classes.

For designers and beginners, black Yu can make an analogy, just likeThree steps:

1. Set all the code in Fla to the first frame,
2. Copy the code in the first frame to an external as file.
3. Put the code in the constructor of this class in the form of AS3.0. Then extract the attributes and methods of some classes based on these codes.

Well, this metaphor has many minor issues. But it is intuitive and easy to understand.
When you understand what's wrong, you no longer need this metaphor.

For program developers (such as c #)
Document Class is like an Entry. Its constructor is equivalent to a main function.

Download tutorial examples:Click to download

Here is an example:

Is the last established KingdaMC. fla and KingdaMC. as still there?
Deleted? Redo it again. This time we will use the link components in fla and related KingdaMC files.

Let's do two things:

1. Create a Document Class.
2. Use the previous Kingda. fla to bind the Document Class.

1. Create an as file named KingdaMCDocumentClass. Remember to be in the same directory as fla:
Enter the following code:

// Because the package is in a unified directory, there is no path behind the package
Package {
Import flash. display. MovieClip;

Public class KingdaMCDocumentClass extends MovieClip {
Private var tempMC: KingdaMC; // temporary variable that holds the reference of the created KingdaMC.
Private var MAX_MCS: int = 10; // maximum number of KingdaMC instances that can be created

// Constructor, with the same name as the class, must be public in AS3.0. Private, protected, or custom namespace cannot be used.
Public function KingdaMCDocumentClass (){
Var I: int; // The new data type int. Use int as long as it is an integer. Efficiency is faster than Number.
For (I = 0; I <MAX_MCS; I ++ ){
TempMC = new KingdaMC ();

// The following two rows define the random KingdaMC shape size.
TempMC. scaleX = Math. random ();
TempMC. scaleY = tempMC. scaleX;
// The following two rows define that the created KingdaMC location is random on the stage.
TempMC. x = Math. round (Math. random () * (this. stage. stageWidth-tempMC. width ));
TempMC. y = Math. round (Math. random () * (this. stage. stageHeight-tempMC. height ));
AddChild (tempMC );
}
}
}
}

It is easy to do. We used the KingdaMC class to create ten KingdaMC instances on the stage, with random sizes and random positions.

2. Use the previous Kingda. fla to bind the KingdaMCDocumentClass.
Make sure that the symbol with KingdaMC class links already exists in the library.

Setting Method


Click to zoom in

Delete all the first frame of code first. Ctrl + Enter to test. Is there 10 MC.
Then add the first frame of code as shown in the figure, and enjoy the stage element. Do not follow the Document Class rules.

So what is it?
Simply put, it is a standard solution to replace the original scattered timeline code.
When a Fla file is compiled, the Document Class is automatically created. We can understand that Flash IDE secretly adds the following code:

// Path indicates the Document Class package, as mentioned in the previous section org. kingda;
// DocClass refers to the Document Class name
Import path. DocClass;
Var TouTouDe: DocClass = new DocClass ();
// The instance name TouTouDe is garbled by me, haha. It may be the default instance1 or another name.

The advantage is that all timeline logics are stripped to Document Class files. Once a file is an independent Class file, it can be easily combined with CVS software. It is a good choice for team collaboration (Designer + Coder. For beginners and small projects, there is no advantage or need.

The following describes some advanced information and application:
What are the disadvantages?
There are many disadvantages. That is quite uncomfortable.
Do not directly name all the components on the stage with attributes (for example, name them BigKingda). Otherwise, the following error message will appear:

ReferenceError: Error #1056: The BigKingda attribute cannot be created for KingdaMCDocumentClass.

Of course, this restriction only applies to the components on the stage. It is no problem to name the child components directly in the component class, and it is no problem to write the relevant code in Timeline.

Important: What is the initialization sequence of Document Class?
The initialization sequence is as follows:
Document Class initialization> main timeline code execution> Class initialization bound to stage components> timeline code initialization of Components> ..

The following layers cycle according to this rule.

OK. The three main features are described in detail here.
It is easy to understand. Black Yu also added some advanced information for reference by laruence. A favorite one! Pai_^

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.