OOP in FLASH (below)
Fourth, Event Handler
The event processing method exists as a method of the class. When an event is triggered, the event processing method is called. Event processing method It is first a method and it can be associated with specific events of an object. Therefore, it should have the general characteristics of the method, so the only difference is that the event processing method has no return value. Events can also be invoked using program analog calls, such as to call the OnRelease event in an onpress event, which is similar to JavaScript. Event processing method usually defines the first frame of the time axis:
VAR P: function = new function (); // declaration and definition a method
P = function () // assigns method variables
{
Trace ("Press");
}
This.onpress = p; // Associate the method and event, this refers to the current timeline
Due to such a mechanism, the event processing method even allows an object to be defined for the dynamically created object. E.g:
_Root.attachmovie ("Symbolid", "NewClip_mc", 10);
NewClip_mc.onpress = function ()
{Trace ("You Pressed Me");
Another mechanism for event processing is called event listening. Event listeners need to understand two concepts: listener objects (Listener object) and Broadcast Object. Broadcast object requires an AddListener () method to register yourself to the listener object. The listener object listens on the event of the broadcast object, and the method of calling related methods after the event occurs. The process is shown in the figure:
Event monitor
Event listening provides a more flexible event reception and processing mechanism. For example, the MC object can be defined to accept the onRelease event of the Stage object, or you can use the Button object to receive the onchange event of the TextField object. Alternatively, multiple broadcast objects can be associated with a listener object, or multiple listener objects can be associated with a broadcast object. Be careful when using event listeners, the handler of the event cannot be an event of an event. The basic method of using event listens is as follows:
ListenerObject.EventName = function ()
{
// Processing code
}
BroadcastObject.addlistener (ListenerObject);
In addition, it is often used in development that the button and movie clip event processing are used. Use ON () or onClipEvent () in the movie clip and buttons to join the event handler directly:
On (Press)
{
Trace ("Press");
}
ON () For button events, onClipevent () is used for movie clip events. Of course, ON () can also be used to process the button event in the movie clip. In the program, you can specify a processing method for multiple events at the same time, and the comma is separated from the event. Using this method directly defines the incident does not contradict the event processing method, they are just an executive issue. Which method for specific use is also depends on personal habits.
Event processing code also has a surplus and scope. The survival of code (mainly variables) in event processing depends on the event definition. When using an event processing method and event listener, since the function is used, the varying period of the variable is consistent with the survival of the variable defined in the function. It should be noted that when using ON () or onclipevent () defining the event processing, the defined variable is considered a time axis variable (whether it is used by a VAR declaration). If the variable is used in the ON () event code in the button, the variable is considered to be the time axis variable of the time axis where the button is located. That is, the variable defined in the movie clip instance belongs to the movie clip, and the variable used in the button belongs to the timeline of the button, which is both _parent. When the event is defined, the THIS keyword is often used, and the keyword is a reference to the current object. This is a different meaning when using different event definitions. In an event handler or event listener, this refers to the object that defines the event function. Using this in On () or OnClipevent (), if the object is the MC, referring to the current MC; if in the button, the time axis of the button is referring to the time axis of the button, it is both _parent. In addition, the button is not allowed to define a frame script, which is allowed to define a frame script in a graphic, but the script is not compiled and executed.
5. OOP in ActionScript2.0
Objects can usually be described in two aspects: a current state, which defines the current state of the object; the other is behavior, which defines how objects are interactive with external input. For objects, a model can be created, this model is called class (Class). The model only implements the representation of the object, but not the object itself. Moreover, each model is defined according to a specific purpose, and there is no full portrait and behavior of the object. Class is a data type that can be defined by the programmer, and some books call this data type as class type. Class is composed of data and its related operations. That is to say, the class defines the type of data type and the type of operation performed on these data. The variables and methods contained in the class are often referred to as class members. In ActionScript 2.0, programmers can customize this data type in an external file. OOP in ActionScript 2.0 is not complete OOP, just partially implementing OOP because it does not support class method overloading. The Super Keyword implements the code in the subclass to the parent class. However, the class in the Flash is a relatively large difference in the class in the usual object-oriented program language, especially those based on the time axis. Because its data, it includes images, sounds, videos, etc. in addition to simple data objects, and the surplus and scope of the object are based on the time axis. The method of the class is not just a simple data processing, but the performance of the time axis behavior - simply, it is a change in audiovisual state on the timeline. This is also determined by the characteristics of Flash, because Flash's focus is in the performance of media information, and this is also a basic feature of RIA.
The classes and interfaces in ActionScript 2.0 can only be defined in an external file, and the external file will be compiled into the SWF file when publishing, exported, tested, debugging. So if you have changed the external file, you need to recompile after saving. Categories can include a constructor, but does not include a destructor, which is similar to Java - using an automatic memory recycle mechanism. The constructor of the class is called automatically when the instance of the class is created. The name of the constructor must be the same name as it. If the constructor is not defined for the class, the system automatically calls the default constructor. A class only contains a constructor, and the constructor is not overloaded. The data is initialized in the constructor or the object event can be defined. The following code defines a Person class: Class Person
{
VAR name: string = "leefangjie";
VAR AGE: NUMBER = 23;
Function Person (MyName: String, Myage: Number)
{
Name = myname;
Age = myage;
}
Function show (): String
{
Return ("My Name IS" Name ", AGE IS" AGE;
}
}
When defining the properties of the class, it is best to define them at the beginning and use the strict data type definition to increase readability. The definition of the method is similar to the creation of the function, and the method can have parameters and return values. It should be noted that the class must be saved in the external file, and the class name must be the same name as the AS file it. By default, class files are required to be saved in the same directory by default, and of course Flash automatically searches class files according to class paths (ClassPath).
After the class definition is complete, you can create an instance of the class. The New operator is used to create an instance of the class. Its syntax is quite simple, the following statement creates a Person class:
Var newPerson: Person = New Person ("Leefangjie", 23);
The inheritance of the ActionScript 2.0 support class is extended using the Extends keyword. Subclasses can inherit the method and attributes of the parent class, and the subclass is also called derived class, and the parent class is called the base class. The syntax of the inheritance is as follows:
Class Subclass Extends Suppover
{
// body of class
}
Here, Subclass is inherited from the support, the subclasses will inherit all methods and properties of the parent class. In inheritance relationship, the parent class can be either Flash's built-in class or a class that the user is defined by the user. If inherited is a class built-in class, this subclass also inherits its method and properties. The method in the parent class can also be redefined in the subclass, and the programmer can redefine a method of inheriting from the parent class as needed, so that the method of the parent class is hidden in the subclass. In ActionScript 2.0, multiple inherits are not allowed, that is, a subclass inherits a plurality of parent classes that are not allowed. ActionScript2.0 also supports interface inheritance, its syntax and class inheritance: Interface IA EXTENDS Interface IB {}.
Like other object-oriented languages, the class members can also be accessed in the class of ActionScript 2.0. By default, class members can be accessed by another class, that is, all class members are public in the default. But in some cases, members of the class don't need to be accessed by other classes, and you need to do access restrictions on the members of the class to make them a private member of the class. Accessing the modifier using the private can prevent other types of access. The private member of the class is only defined in the class and the subclass of the class that defines its class. So the private member of the class is only accessed at the class level, and cannot be accessed at the instance level. The class can also have a static member and declare with Static keywords. Static members belong to the class itself, not an instance of the class. Static members are unique, do not create a copy when creating objects, and shared by all objects of the class. If you want to access a static member of the class, you must reference the class name. Members of the MATH class are static members, so you can use the class name directly to access instances. Also relative to instance members, they belong to an instance of the class, access to the object name.
Classs For example, it is considered static, so there is only one copy during operation, and the class can have multiple copies. The visits between static members and instance members are restricted, and static members cannot access instance members, while instance members can access static members. In this way, the static member of the class is shared by all instances being class.
In object-oriented languages, interfaces are used to declare class members, but do not define its specific implementation. In ActionScript2. The interface is composed of some empty methods. When developing a large application, usually, there is little link between different classes, but there must be a mechanism to communicate between different classes, which can be implemented by interface definitions. . The interface is a protocol that must be followed by all classes.
One way to implement communication between classes and classes is to define a class containing all methods, and other classes can be inherited from this class. Since the application itself consists of some unrelated classes, using this method, forced to define such a general class is obviously not suitable. A good solution is the definition of the interface, declare those methods that are about to communicate, and then implement these methods in the specific class. In many cases, you need to design a class structure very carefully, but after you understand the interface, use it in the program, the interface can make the program better structure, maintainable and scalability.
The interface is implemented by a class, you need to use the imports keyword. Moreover, the class must implement all the methods declared by the base interface of the current interface. The following is the code implemented by an interface: Class B Implements IAB {}. The interface does not have a variable declaration or assignment operation, and the interface cannot include braces, the interface is only the method declaration. Like the class, the interface defines a new data type, any class that implements the interface can be considered to be the data type defined by the interface, which can be used to determine if the given class is the implementation of the interface.
In order to use a defined class or interface, Flash must determine the location of these external files. Flash is called classpath in which Flash is searching and interface definitions. When an external file is created, it must be saved to a specified path, or in a subfolder under this path, otherwise Flash cannot determine the location where the class or interface is located. The subfolder created in the catalog of class paths is named package (package) to organize classes or interfaces.
Flash has two ways to set class paths: Global ClassPath and Document Class Class Path (Document-Level ClassPath). The global path is applied to the external AS file and the FLA file, set in the Preference dialog box. The classpath of the document level is applied to the FLA file and sets the Publish Setting dialog. By default, the global class path has two: the relative path of the path where the current document is located, and the installation path of the flash. The class path of the document is empty in the default. After the class file is defined, it is necessary to be referenced in other external files or FLA files. When compiling, Flash To search these class files, first it is found according to the document class path specified by the current document, if this file is not found, or The classpath is empty, and Flash is used to search for the global class path. If you still have not found a compilation error. If there is no associated document class path, Flash is only searching in the global class path. Package is used to organize an external script file. The package is a folder containing several class files, and the package can contain other packages, which is called subcap (Subpackage). If you want to reference the classes in the package, you can use a fully qualified name or import the package using the Import statement. Using the classes in the script, you must use the package name as a prefix, that is, if you use a fully qualified name, if the class is on the top directory, then its fully qualified name is its class name. When specifying the path of the package, use the point to separate the package, between the packets and the packets, and the points are used to illustrate the nested folders. Use the import statement to import a package in the script, or you can use all the classes in the package in an asterisk (*). After the package is imported into the code, there is no need to use a fully qualified name when the object is created.
In Flash ActionScript, there is also a special class called Dynamic Class. By default, the properties and methods of the class are fixed. That is, the instance of the class cannot create or access an attribute or method that is not declared or defined by the class, otherwise the compiler error will occur. However, in some cases, you may need to add and access an undefined attribute or method in the original class definition at runtime. This access can be implemented by the Dynamic class qualifier. Because it is a dynamic class, there is no compiler error. It should be noted here that the object being created is also considered dynamic, and a new object member can be created without using the strict data type definition. Interestingly, in this case, it can even redefine the functionality in the runtime, which is also a certain polymorphism.
6. Associate the class with the library project
The purpose of introducing classes and interfaces in Flash ActionScript is largely intended to manage organizational management of code. In the early versions, the code is almost written in the FLA file, and the code is very dispersed, it is difficult to manage, the readability is extremely poor. The use of the class has been separated from the original frame scripts and event scripts from the original code. Define attributes, events, and methods separately in the class.
The library project is defined in the Linkage dialog of the library project. Although, movie clips, buttons, and graphics can set the linkage dialog, but most commonly used and core library projects or movie clips. So only the connection between movie clips and classes is discussed here. In the Linkage dialog, you must first select "Export for ActionScript" option, Identifier specifies the script reference name of the library project, and the script class (as2.0 class) specifies the associated with the library project. Class. When class definitions, the class associated with the movie clip must be extended from the MovieClip base class, which is also obvious. Since the timeline is a nested tissue relationship, and the object on the timeline can be dynamically created in the editing environment, it is necessary to implement the control of the time axis under the time axis, and it must be defined in the class member. You can specify a name for an instance when editing status and runtime, and the definition of a reference name in the class should be the same as the specified name. The event processing method of the object needs to be defined in the constructor, and the definition of the event processing method is consistent with the definition of the function. In addition, in the constructor of the class, the event processing method can also be defined for the component of the class. But here should be particularly careful and pay attention to this and _parent. _parent attributes can only be used based on the time axis object, otherwise the reference is invalid. The continuous frame rate trigger of the OnenterFrame event can be defined for the frame script.
The method associated with the class library project is relatively simple. The side focus of this process is still the definition of the creation of library projects and class files. In addition, components are movie clips that have been compiled, apparent that they have meaningless associations with classes.