[AS2] dynamically create V2Component

Source: Internet
Author: User

Can V2Component be dynamically created? The answer is clear.
V2Componet inherits from UIObject, while uIObject inherits from MovieClip.
Therefore, the attributes and methods in MovieClip are as easy to use in v2Component,
It is natural to use attachMovie to dynamically load V2Component.
But this is not the end, isn't it? The strongest listener in V2Componet hasn't been used yet.
What are you going to do?

SomeComponent. addEventListener ("eventName", listenObj );
ListenObj. eventName = function (): Void
{
//....
}

It looks good. If the code is written in the main scenario in the method of peer programming, the problem will not be too big.
However, when the engineering volume changes, it is troublesome to manage it. You will say, I create a dedicated Component class to manage it.
It is a good idea to create, register, and destroy listeners. If you only have one
Scenario: V2Component is generated only once in the entire program, and there is no problem at all. But when your
When the program needs to dynamically change the corresponding components according to different situations, it will be very troublesome,
Your all-powerful class will become unusually large, reducing maintainability to a minimum.
In fact, it is easy to handle, that is, divide and conquer, and create the effect when the effect appears.

My current practice is to create a sub-package of the class, called Com,
The specific implementation is put into another sub-package that specifically stores message processing, called Msg.
Therefore, you can imagine the situation as follows:
Com + ---
ComScene
ComEffect1
ComEffect2
...
Msg + ---
MsgScene
MsgEffect1
MsgEffect2
...

In fact, such a seemingly one-stop action brings great convenience to the modification and expansion of the entire program.

The specific implementation code is extracted as follows:
Class Sdgp. SEffect. CS2BallConserve extends CSEffectBall1D
{
Public function CS2BallConserve () // create its own ComMag object in a certain effect class
{
MComMag = new CSCom2BallConserve ();
// The declaration in the parent class is private var mComMag: ISComMag;
// ISComMag is the interface class of all ComMag classes.
...
}
.....

// Create a component.
Public function init (): Void
{...
ThisP. mComMag. create ();
...
}
}

// Directly manage the component class:
Class Sdgp. SCom. CSCom2BallConserve extends RCSComMag
{Public function CSCom2BallConserve ()
{
MMsgF = new CSMsg2BallConserve ();
// Declare the Class Object private var mMsgF: ISMsgF in its parent class; point to the abstract interface
}
Public function create (): Void
{

.....

_ Root. attachMovie ("TextInput", "txtm1", 14 );
_Root.txt m1.move (20,200 );
_Root.txt m1.restrict = "^ A-Z ^ a-z ";
_Root.txt m1. _ width = 50;

_ Root. attachMovie ("TextInput", "txtm2", 15 );
_Root.txt m2.move (20,260 );
_Root.txt m2.restrict = "^ A-Z ^ a-z ";
_Root.txt m2. _ width = 50;

....

ListenerAdd (); // listener Registration
}
 
Public function remove (): Void
{...
Eval ("_root.txt m" + 1). removeMovieClip ();
Eval ("_root.txt m" + 2). removeMovieClip ();
...

}
Private function listenerAdd (): Void
 
{
_ Root ["txtm" + 1]. addEventListener ("enter", mMsgF );;
}
.....
}

// Delegate the message processing of the component to the Msg class for processing:
Class Sdgp. SMsg. CSMsgSce extends RCSMsgF
{
Public function CSMsgSce ()
{
Enter = msgEnter;
}

Private function msgEnter ()
{
If(evt.target1_1__root.txt m1)
{
// Proccess
}

}

I wrote the code as a step-by-step way to show the idea of level-by-level delegated distribution.
The biggest benefit of constructing a program structure in this way is the convenience of debugging and expansion.

By the way, I almost forgot. Many people may ask if there is any need for dynamic component creation. My answer is yes,
This is even more important if you need to update components in the program frequently based on different situations.
This is because we want to use code to control programs without being constrained by the interface.
Are you sure you want to talk about Visual Basic? Hold on ~~~~

There must be something wrong with my method like this. The level of time cannot be done, and I still hope to have a point or two.

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.