Flash component application and development (below)

zhaozj2021-02-16  67

Flash component application and development (below)

Fourth, Flash V2 Component Development

In Flash MX Professional 2004, developers can create new V2 components yourself in your project. Before starting to develop V2 components, you must first learn about the MovieClip class, the UIObject class, and the UIComponent class because they are the base class of the Flash V2 component. All components comes with all flash are their direct or indirect subsides. Developing a component can inherit from the original parent class, extend the existing component class, so that the created components are further expanded on the basis of the parent class. Alternatively, you can also create components that do not have parent class to implement some other functions. Here, you will focus on how to extend existing components classes. Extend an existing component class, usually include the following steps:

1. The component first is created as a movie clip, and two layers are created in the movie clip, separately for component interface elements and scripts, respectively. Then select the Component Defination in the drop-down menu of the library, enter the fully qualified name of the class to be associated with the component in the AS2.0 Class in the dialog. When the interface element definition is complete, you can create the properties, events, and methods of the components in the external file. Of course, the interface element of the component can also be created using code dynamics in the class.

2. When defining component class files, you must first import the desired class. As mentioned earlier, the MovieCliP class, the UIOBJECT class, and the UIComponent class are the base class of the V2 component, so if the currently created component is any component from the V2 component. Extension, then these three base classes must be understood, and the relevant components below. About component features You can view the component dictionary published by Macromedia, or view class file code directly in the FlashInStaldir / EN / FIRST RUN / CLASSES directory.

3. After determining the class to which the component is to be expanded, the constructor is written for the component class class. In general, the constructor is suggested that the object properties can be defined using an object's attribute interface. In addition, depending on the initial call order, it is sometimes setting attributes in the constructor that can cause overwrite default values.

4, next to the addition of the version of the component, if the currently developed component is part of the component package, then put the version information into another external file. When defining version information, you can inherit the static string property of the UIObject class. There is also a need to define three important properties: SymbolName, Symbolowner, and ClassName; they are inherited from the component base class. SymbolName is defined as a static string variable for specifying the name of the component symbol; Symbolowner is defined as a static Object type, which is a fully qualified name of this class, which will be used in the internal call of the CreateClassObject () method; ClassName definition The component name name.

5. After these are completed, the properties and methods related to the component function are started. When the properties and method definitions, the preferred approach is to determine the access feature of each attribute and method, so that the component provides a good application interface (API). So use private and public to declare the accessibility of properties while defining properties and methods, and use the setter and getter functions to set and get the value of the property. This makes it better to achieve the package package, and users do not need to understand the internal details of the components.

All components must implement two core methods: initialization methods and size adjustments. Flash Player may generate an error if it is not overwritten in the class file of the custom component. Flash calls the initialization method when creating a class. The initialization method should call the initialization method of the parent class, because only Width, Height, and other movie clip parameters can only be set correctly after calling this method. Function init (void): void

{

// Call the initialization method of the parent class

Super.init ();

/ / Add initialization code related to this component here

}

The method of size adjustment is also similar to the initialization method:

Function size (void): Void

{

Super.size ();

/ / Add the size adjustment code related to this component here

}

6. In order to make the property can be seen in the development panel, you must also declare the metadata (Metadata) related to the attribute statement. Metadata tags can define component properties, data binding properties, and events. Flash can explain these statements and update the development environment accordingly. Metadata is related to class declarations or individual data fields. The metadata statement is bound to the next line of the script. For example, when defining component properties, add metadata tags in the prior row of the attribute declaration. Add a metadata tag to the class definition when defining the component event to bind the event to the entire class. For properties, there are two important metadata: inspectable and inspectablelist.

Inspectable metadata defines the properties of components displayed to the user in the Component Inspector panel. The syntax is as follows:

[Inspectable (value_type = value [, attribute = value, ...])]

Property_Declaration name: Type;

Inspectable metadata also includes several metadata tags:

· Name: Type String (optional), the display name of the property in the development panel.

· TYPE: Type String (Optional) Specifies the type of property. If omitted, use the type of properties. The following is acceptable: Array, Object, List, String, Number, Boolean, Font Name, Color.

· DefaultValue: Type can be String or Number (required). Specifies the default value of the property.

· ENUMERATION: Type String (optional). Specifies a list of legitimate values ​​separated by comma.

Category: Type String (optional). Divide properties into a particular subcategory in the Property inspector.

The InspectableList metadata keyword is used to specify which subset of check parameters should be displayed in the Property inspector. InspectableList can be used in combination of Inspectable, which hides the inheritance properties of the subclass component. If you don't add an InspectableList metadata keyword, all checkable parameters (including the check parameters of the component parent class) are displayed in the Properties Checker. The syntax is as follows:

[InspectAbleList ("Attribute1" [, ...])]

// Class Definition

The InspectableList keyword must be defined next to the class and before it is applied to the entire class.

7. Define the event of the component, first use the Event metadata keyword declaration event. Event metadata keyword is used to define component events. The syntax is as follows:

[Event ("Event_name")]]]]]]

In the class file, you must add an EVENT statement to the class definition to bind them to the class, not a particular member of the class. First component class will inherit the base class event. If the base class of the current definition component is UIComponent, 28 events of the MovieClip class, the UIOBJECT class, and the UIComponent class have been included. But in fact, these 28 events are not available. It is very simple because the components can have its constituent structure, and there may be components or other components instances inside the component. Components To provide users with a unified interface, some events of their internal structures must be extracted and defined as components of components, which is an event based on components. For example, the components include two text boxes T1 and T2. At this time, according to the functionality of the component, the CHANGE event of T1 can be issued to the PP event of the component, and the Change event of T2 is published as the component's UU event, of course, about component events. Names can be defined by component developers. So how do you define a new event for the component? For the example above, you can do the following definition:

// Import class

Import mx.core.uicomponent;

// Declared component event with metadata

[Event ("pp")]]]]

[Event ("uu")]

/ / Indicate that the class is inherited from UIComponent

Class T_T Extends Uicomponent

{

// Create two input texts in the editing environment and name it in the class.

VAR T1: TextField;

VAR T2: TextField;

// Define constructor

Function T_T ()

{

/ / Publish T1 Change Events in the constructor

T1.onchanged = function ()

{/ Create an event object, store information related to event

Var EventObj = new object ();

/ / Define the name of the event type

EVENTOBJ.TYPE = "PP";

/ / Indicate an object of event broadcast (occurrence)

EVENTOBJ.TARGET = _PARENT;

// Publish the event as a component event

_Parent.dispatChevent (EventObj);

}

// Release T2 Change event in the constructor

T2.onchanged = function ()

{

Var EventObj = new object ();

EVENTOBJ.TYPE = "uu";

EVENTOBJ.TARGET = _PARENT;

_Parent.dispatChevent (EventObj);

}

}

}

In the above code, first declare the two event uu and pp of the component, and then define the Change events of T1 and T2 in the constructor of the component class, and use _parent.dispatchevent in their Change events. EVENTOBJ); statement publishes chang events as component events. The DispatChevent () method requires an Object type event object as a parameter that saves the information related to the event: TARGET indicates the object of the event broadcast (occurred); Type defines the name of the event type - can also be considered an event name . In the event script, you can respond to the event, which is the same as the general event processing method:

/ / Respond to the component's PP event

ON (PP)

{Trace ("pp");

// Respond to the component's UU event

ON (UU)

{Trace ("uu");

The changes in the properties of the components can also be released as an event, for example:

Private var TM: String; // Publish components in Setter

Public Function Set TTM (Val: String)

{

TM = VAL;

Var EventObj = new object ();

EVENTOBJ.TYPE = "KK";

EVENTOBJ.TARGET = THIS;

This.dispatChevent (EventObj);

}

Of course, it is also necessary to use Event metadata to declare events. But in fact, event components without EVENT metadata declaration can also respond. Event metadata declaration seems to be just a code prompt. Therefore, metadata is only a more friendly user interface for components, making developers easier to use components.

You can customize an icon for a set before publishing the components. The icon size is required to be 18 x 18 pixels and saves it as a PNG format. Its Alpha transparency must be 8 bits, the pixel requirements in the upper left corner are transparent to support the mask. There is also a need to define the addition metadata declaration in the component class file:

[Iconfile ("Component_name.png")]]]

Like event declarations, the declaration must be placed to the component class before the class definition. Finally, the image is saved in the same directory where the FLA file is located. When exporting the SWC file, Flash will automatically contain the image.

When the component is defined, after the test is passed, the component can be published for other developers. Flash MX 2004 exports components as component packages (SWC files). When publishing components, you can just provide SWC files to other developers. This file contains all code related to the component, SWF files, images, and metadata, so other developers can easily put it in their own Flash development environment.

Here is a preliminary discussion on Flash V2 components. At the time of specific development, the properties, events, and methods of the components should be graphically compiled according to the functional characteristics of the components, and the metadata definitions of user interfaces are declared. If the component is a visual component, you need to make a graphical element of the component interface for the component.

转载请注明原文地址:https://www.9cbs.com/read-22690.html

New Post(0)