Time Axis and documentation

Source: Internet
Author: User

The root of as3 is that the top-level container is stage, and stage is the only instance of Stage class. When you create a document, you create a stage instance. The root mentioned above is a visual instance in stage.
Because both stage and root are containers, when you write code on the timeline, you can have two options to add objects under stage or root.

We first write the code in the timeline:

 

Trace (stage. numchildren) // 1
Trace (stage. getchildat (0) // [object timeline0_6d54148745b4f34c992852913c20679d]

 

 

The output information tells us that one display object in stage is root, and the root is in stage, and the root is an instance of Timeline0 class.

Next let's take a look at the document class.

 

Code

Package
{
Import flash. display. Sprite
// The document class must inherit the sprite class or its subclass (such as movieclip)
Public class documentclassexample extends Sprite
{
VaR var1 = 5
Function documentclassexample ()
{
Trace (stage. numchildren) // 1
Trace (stage. getchildat (0) // [object documentclassexample]
}
}
}

 

The output information tells us that there is only one display object under the stage. This Display object is no longer a root, but an instance of the DocumentClassExample class, that is, root is replaced. If the document class inherits the Sprite class, you cannot write code on the timeline, because the Sprite class does not have a timeline. If you want to write the representation in the timeline, you must inherit the MovieClip class.

Modify the Code:

 

Code

Package
{
Import flash. display. Sprite
// The document class must inherit the sprite class or its subclass (such as movieclip)
Public class documentclassexample extends movieclip
{
VaR var1 = 5
Function documentclassexample ()
{
Trace (stage. numchildren) // 1
Trace (stage. getchildat (0) // [object documentclassexample]
}
}
}

 

In fact, if you select MovieClip as the base class, the problem will be returned to the Timeline0 class in the previous section. The Timeline0 class inherits the MovieClip class, And the DocumentClassExample class also inherits the MovieClip class, both of them can automatically create an instance. The former is root, and the latter is [object DocumentClassExample]. Although the names are different, you can use this to reference them on the timeline and test them:

 

Trace (this) // [object documentclassexample]

 

 

That is, the Timeline0 class is also a document class, but it is automatically created, and the DocumentClassExample class is customized.

Now, let's try the test code in Section 1:

The document class is changed to the following:

Package
{
Import flash. display .*
Public class documentclassexample extends movieclip
{
VaR var1 = 5
Function documentclassexample ()
{

}
}
}

 

Then write the following on the timeline:

VaR var1 = 8

 

During the test, a name conflict is prompted, that is, var1 is defined twice. Well, var1 is defined twice in the DocumentClassExample class. As we mentioned above, a variable can be defined twice in the same namespace.
Let's change the timeline code:

VaR var2 = 8

 

Then change the document class to the following:

Package
{
Import flash. display .*
Public class documentclassexample extends movieclip
{
VaR var1 = 5
Function documentclassexample ()
{
Trace (var1, var2) // 5 undefined
}
}
}

 

 

Why is it undefined? I think it is about the initialization order. That is to say, first execute the constructor and then execute var var2 = 8. In this case, we add a method to the document class to access var2, which ensures the initialization order.
Modify the document class:

Code

Package
{
Import flash. display .*
Public class documentclassexample extends movieclip
{
VaR var1 = 5
Function documentclassexample ()
{

}
Public Function _ trace ()
{
Trace (var1, var2)
}
}
}

 

Then test on the main timeline:

VaR var2 = 8

 

// Test the function directly in the class.

_ Trace () // 5 8

 

// Pass the instance Test

This. _ trace () // 5 8

 

 

Because the constructor executes the code first and then executes the timeline, you can certainly use this. the _ trace () method is used for access. Why does the _ trace () method work, just as the variable defined by var is added to the document class () this is equivalent to testing in the document class.

Code for deleting the timeline, written:

Trace (var1) // 5

 

Oh, when I access the class to define variables, the problem is returned to the content in the previous section.

In the previous section, in addition to using var to add variables to the Timeline0 class, you can also add instance-specific variables in the form of root.. Let's test the following:

This. var2 = 8

 

During the test, the system prompts that the var2n attribute cannot be created.

Cause: although the MovieClip class is a dynamic class, And the DocumentClassExample class also inherits the MovieClip class, the DocumentClassExample class is not a dynamic class. The following is a sentence found in help:
The subclass of the dynamic class is also dynamic, but there is one exception. By default, the MovieClip class subclass is not dynamic, even if the MovieClip class is dynamic

Well, find the cause and change the DocumentClassExample class:

Code

Package
{
Import flash. display .*
Public dynamic class documentclassexample extends movieclip
{
VaR var1 = 5
Function documentclassexample ()
{

}
Public Function _ trace ()
{
Trace (var1, var2)
}
}
}

 

 

Test again. Normally, attributes are successfully added to the instance.

 

 

 

 

 

From: http://space.flash8.net/space? 84320/viewspace-325525.html

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.