As the program grows, we need to split it into multiple SWF and dynamically load it as needed. During splitting, we should try to compile different classes into a unique SWF to avoid increasing the file size of the entire program due to the increase in SWF files. According to this principle, the following two kinds of SwF can be split, and their code and resources can be shared with applicationdomain.
Module)
According to the program logic, you can split up multiple "function modules", such as "Registration" and "management". According to the levels or scenarios of game or community programs, different "scenario modules" can be split ".
These modules are not required for the main program to run. They are only loaded as needed.
Runtime shared library (RSL)
Resources used in the main scenario or multiple modules, such as bitmaps, sounds, and designed page elements, can be loaded as a "library" before the main program runs. You only need to load one set of skin (skin) that can be replaced.
----------------------------------------------
Applicationdomain attributes
Public var applicationdomain: applicationdomain = NULL
Specifies the application domain used for the loader. Load () or loader. loadbytes () method. Only SWF files written using ActionScript 3.0 should be loaded.
This attribute is used only when an image is not used, or when a SWF file is written using ActionScript 1.0 or ActionScript 2.0.
Each
Security domains are divided into one or more application domains represented by the applicationdomain object. Application domains are not for security purposes; they are used to manage
The collaboration unit of the Event code. If you load a SWF file from another domain and allow it to be placed in another security domain, you cannot control the load.
. However, if you load the SWF file to your own security domain (because
This SWF file comes from your own domain, or you are importing it to your security domain), you can control which application domain to select for the loaded SWF file.
In loadercontext. applicationdomain, you can only pass the application domain in your own security domain. If you try to pass the application domain in any other security domain, a securityerror exception is thrown.
There are four applicationdomain attributes available for you to choose from:
Add
The sub-level (module) of the applicationdomain of the loader ). Default value. Available languages
Method new applicationdomain (applicationdomain. currentdomain) explicitly indicates this option. This will allow
The loaded SWF file directly uses the parent class. For example, you can use it by writing new myclassdefinedinparent. However, the parent level cannot use this
Syntax; if the parent class uses a child class, it must call applicationdomain. getdefinition () to retrieve them.
The advantages of this choice are:
1. If the class defined by the child level has the same name as the class defined by the parent level, no error results will be returned;
2. The child level only inherits the definition of the class from the parent level, unless the child level or parent level calls applicationdomain. getdefinition () method to retrieve the child-level conflict definition. Otherwise, this definition is not used.
The applicationdomain of the loader itself (Shared Library)
. Use this application domain when using applicationdomain. currentdomain. After loading is complete, the parent and child can directly use the peer class.
If the class to be defined by the child level has the same name as the class defined by the parent level, an error will occur and the loading will be abandoned.
System
Sub-Level of applicationdomain (a program or module that runs independently ). Use new applicationdomain (null)
This application domain. This completely isolates the loaders and The loaders, allowing them to define classes of their respective versions using the same name without conflict or hiding. The only way for one party to view the other's classes is
Call applicationdomain. getdefinition.
Its
Its Sub-Level of applicationdomain. Sometimes there may be more complex applicationdomain hierarchies. You can extract the SWF file from your
Your securitydomain is loaded into any applicationdomain. Example
For example, new applicationdomain (applicationdomain. currentdomain. parentdomain. parentdomain)
To the new Child of the parent level of the current domain.
After loading
With applicationdomain. getdefinition (), either party (the loader or the loader) may need to find its own
Or the applicationdomain of the other party. Either party can enable
Use applicationdomain. currentdomain to retrieve references to its own application domain. The SWF file to be loaded can be connected
Through loader. contentloaderinfo. applicationdomain to retrieve the loaded SWF File
. If the SWF file to be loaded knows its own loading method, it can find the SWF file to be loaded.
Applicationdomain object. For example, if the sub-level is loaded by default
Use applicationdomain. currentdomain. parentdomain to find the application domain of the SWF file to be loaded.
----------------------------------------------
Applicationdomain is a container that stores as3 definitions (including classes, methods, interfaces, and so on.
When using the loader class to load SWF, you can specify the applicationdomain parameter to load SWF to different domains ):
Code
VaR Loader: Loader
=
New
Loader ();
VaR context: loadercontext
=
New
Loadercontext ();
//
Load to subdomains (modules)
Context. applicationdomain
=
New
Applicationdomain (applicationdomain. currentdomain );
//
Load to the same domain (Shared Library)
Context. applicationdomain
=
Applicationdomain. currentdomain;
//
Load to new domains (independent programs or modules)
Context. applicationdomain
=
New
Applicationdomain ();
Loader. Load (
New
URLRequest (
"
Loaded.swf
"
), Context );
Applicationdomain uses a tree structure similar to displaylist. Relative to stage
(Stage), you can think that the root of applicationdomain is the system domain, the package
Including the definition of the core class of Flash Player. The domain where the main program is located (hereinafter referred to as the main domain) is its unique subdomain, similar to the document class under the stage
(Document class ).
Code in a FLA document class:
This. Stage
. Addchild (mysprite );
This. addchild (mymc );
This. addchild (myshape );
Display list after running:
Similar structures of applicationdomain:
Load to subdomains (modules)
Similar to "inheritance", subdomains can directly obtain all class definitions of the parent domain, and vice versa. Different from the inheritance relationship, if a subdomain has a class with the same name as the parent domain, the subdomain definition is ignored and the parent domain definition is used.
Load to the same domain (shared library during runtime)
Similar to the merging relationship in a set. All class definitions in the loaded SWF are merged into the current domain and can be directly used. The definition with the same name as the current domain is ignored when it is loaded to a subdomain.
Load to new domains (independent programs or modules)
Before SWF is loaded into a specified domain, you must check whether the domain and its parent domain have classes of the same name. Duplicate definitions are ignored. If you load programs written by others or use the old version of the main program to load the new version of the module, to avoid class name conflicts, You need to load the new domain to run independently to use your own class.
Can I load a module to the same domain? Why do we need to load data to a subdomain? The advantage is that when you Uninstall a module loaded to a subdomain, you only need to ensure that all references to this module are cleared, and all the class definitions of the module will be reclaimed (garbage collection ).
You can access applicationdomain in two ways:
Applicationdomain. currentdomain
Currentdomain is a static variable of applicationdomain, indicating the domain where the current code is located. This variable is very strange. It points to the main domain in the main program, and points to the subdomain where the module is located in the module loaded to the subdomain. Although applicationdomain has a parentdomain
Owner
But the subdomain has automatically obtained the class definition of the parent domain, so you can get the parent domain definition through applicationdomain. currentdomain-including the main process
Sort and load the shared libraries to the primary domain. (Note: The system domain cannot be directly accessed. The parentdomain attribute of the primary domain and all new domains, that is, the system domain subdomain, is null)
Applicationdomain attribute of loaderinfo class
This method can access the applicationdomain of SwF loaded in any way. For the main program, the library definition loaded to the same domain already exists in applicationdomain. currentdomain, while the module class main program is generally not used. Therefore, this method is not recommended for individuals.
The hasdefinition () method of applicationdomain determines whether a definition exists. The getdefinition () method gets the specified definition. The following uses an example to describe how to use applicationdomain and how to split an application.
Here, there are four SWFs, and shell.swfis the main program. lib.swfis the shared library. login.swfand result.swf are the "login" and "result" modules respectively. All view components are in the shared library.
In actual development, there may be many libraries, such as "bit library", "sound-Effects Library", and "general model library. The "general library" stores resources shared by multiple modules, such as the background element in this example.
The unique resources of each module are stored in their respective SWF.
The main program first loads the Shared Library to the same domain, and then loads the "login module" to the subdomain. The main program can operate on loaded modules like ordinary visual objects:
Listen to events and call methods. The compiler does not recognize undefined classes. To use a strong type, we recommend that you define corresponding interfaces for the primary class and model and use a small amount of repeated code to assist programming.
Code
Private
Function showmodule (p_module: imodule ):
Void
{
If
(
This
. M_modulelist [
0
]
=
"
Login.swf
"
)
{
P_module.show (
This
);
P_module.addeventlistener (
"
Login
"
,
This
. Onlogin );
}
Else
{
P_module.show (
This
,
This
. M_username );
}
}
The module "inherits" all the classes and resources of the main program and the shared library. You can use applicationdomain. currentdomain. getdefinition () to obtain the corresponding classes. Note that a referenceerror will be thrown when a nonexistent class is obtained.
Code
Protected
Function getclass (p_name: string): Class
{
Try
{
Return
Applicationdomain. currentdomain. getdefinition (p_name) as class;
}
Catch
(P_e: referenceerror)
{
Trace (
"
Definition
"
+
P_name
+
"
Does not exist
"
);
Return
Null
;
}
Return
Null
;
}
Log on to the module to obtain the interface elements in the library, and throw an event after clicking the button. The event class does not allow parameters. A Custom Event that inherits the event must be used to throw a parameter. Master
The program can also compile the custom events of the module (this increases the file size of the entire program), or let the function of the listening module event accept an objcet parameter, to obtain its dynamic attributes.
Code
Private
Function onlogin (P_e: Object ):
Void
{
This
. M_username
=
P_e.username;
VaR login: imodule
=
P_e.currenttarget;
Login. removeeventlistener (
"
Login
"
,
This
. Onlogin );
Login. Dispose ();
This
. Loadswf ();
}
After receiving the event, the main program uninstalls the registration module, loads the "result module" to the subdomain, and sends the "username" parameter sent from the logon module to the result module.
Code
Public
Function show (p_parent: displayobjectcontainer,
Rest ):
Void
{
VaR libclass: Class
=
This
. Getclass (
"
Net. eidiot. appdomaindemo. libaray
"
);
If
(Libclass
! =
Null
)
This
. Initui (libclass, rest );
}
Override
Protected
Function initui (p_libclass: Class, p_rest: Array
=
Null
):
Void
{
This
. Addui (
This
. Getclass (p_libclass.bg_name ),
"
Result
"
);
VaR resultfunc: Function
=
P_libclass.getresult;
VaR Username: String
=
P_rest [
0
];
This
. Addchild (resultfunc (username ));
}
Note that the initui () method uses the static attribute bg_name and static method getresult () of the libaray class in the shared library respectively (). However, an error is reported when you directly call this static method. You can use the resultfunc variable to retrieve this method.