The fourth part of OOP in Flash
First, ActionScript 2.0 overview
In the latest Flash MX 2004, Macromedia launched a new generation of scripting language ActionScript 2.0. ActionScript2.0 has been adjusted on the basis of ActionScript1.0, providing some powerful programming features similar to other programming languages. ActionScript 2.0 advocates that can be reused, retractable, and maintained. It also shortens the development time by providing users with full coded assist and debugging information. ActionScript2.0 is in line with existing standards and is recommended based on ECMAScript 4.
The appearance of Action Script 2.0 does not mean that the original Action Script 1.0 is eliminated because Action Script 2.0 is just a more rigorous programming language for developers, easy to develop, easy to compile, with ActionScript2.0 written Flash, It can also be supported by Flash Player 6 because ActionScript1.0 and ActionScript 2.0 are compiled as the same type of byte code (Byte-code). ActionScript2.0 In addition to programming grammar, the concept has some differences, it must also be defined in the * .as's external file and is only supported by the professional. Traditional programmers may prefer to use ActionScript 2.0.
Under normal circumstances, there are two scripts in flash: frame scripts and event scripts. The frame script is in a critical frame and is executed when playing the script where the script is played. Event scripts require event trigger to be executed, such as mouse events, keyboard events, data downloads, etc. The distribution of scripts is very dispersed, difficult to manage, and developing large applications is almost unlikely. Since ActionScript 2.0 is required to define external files, and Flash MX 2004 Professional provides effective editing tools and management tools, making code management and software development make more convenient and efficient.
The use of scripts in Flash is obvious. The script can process the data, more importantly, implement the control of the object on the time axis, thereby interacting with the user, which is also one of the basic features of RIA. The classroom is further optimized in FLASH to centrally manage the code in the * .as file. At the same time, the entire software also presents a inheritance structure and is more modular on the software composition.
ActionScript 2.0 uses a Unicode character set, which can use the language of different countries in the same Flash document. Of course, you can also specify the desired character set, the developer can choose UTF-8 encoding or use the default encoding. UTF-8 is an 8-bit Unicode encoding, and the default setting is the character encoding used by the operating system. In addition, ActionScript2.0 is a case-sensitive, uppercase and lowercase identifiers have different meaning.
Second, data type
The data type describes the type of the value stored in the variable. There are two data types in ActionScript 2.0: Simple Type (Primitive) and Reference Type (Reference). Simple types include String, Number, Boolean, which saves actual data. The reference types include MovieClip and Object Types. They are just references to actual data. There are also two special types of NULL and undefined types.
1, String Data Type: The String Type is a simple data type. String Data Types Support 8-Binary escape, 16-Binary escape and double-byte (16-bit) unicode escapes. The String class provides a string processing method that processes the string. Of course, it provides fewer methods, and the function is relatively single, far less than Java's STIC. 2, Number Data Type: Number Data Type is the number of floating point in double precision. Directly use operators to implement simple operations of data for Number types, and you can use Math and Number classes to achieve more complex calculations. The Number class only provides two ways: Number.toString () and Number.Valueof (). Its complex numerical calculation is also dependent on the Math class.
3, Boolean Data Type: Boolean object is a logical value, True or False, ActionScript converts them to 1 or 0 in a suitable case. It often uses the logical operator to control the process of the program.
4, Object Data Type: Each Object Object is a collection of properties, each attribute is a set of name values, and the value of the property can be any data type in Flash. Specify objects or properties, you need to use a point (.) Operator. For example, building is an object, and the object floor is a property of building, and Room is a FLOOR attribute, which has a nested organizational structure. If you want to access the ROOM property, you can use the following syntax: building.floor.Room = "12-318"; For developers, the most important role of Object type is to organize information in the Flash application. Creating custom objects To organize various information into different modules, simplify scripts, and improve the reuse rate of code. Of course, more information will be organized in OOP because classes have simpler definitions and methods. The class types in Flash ActionScript will be discussed later.
5, MovieClip Data Type: Movie Clip (MovieClip) Playing an animation in Flash, the MovieClip data type is the only type of data that uses graphic elements, and is a kind of core in Flash. Movie clip objects can use the methods provided in the MovieCLIP class to implement certain controls.
6, NULL data type: NULL data type has only one value, NULL. This value means "no value". NULL data types can be used in many cases, for example:
· Check if an object has received data;
• Check if an object contains a value;
· Check if the function has a return value;
· Check if the parameters of the function are omitted;
7, undefined data type: undefined data type has only one value, undefined. This value is used to specify the value that is not assigned.
In FLASH, it is not necessary to declare which type of data defined is, when assigning values for variables, Flash automatically specifies its data type, which is called Automatic Data Typing. It corresponds to the strict data type (Strict data type). In ActionScript 2.0, it is allowed to declare the variable type when the variable is defined, which is called a strict data type. Since the data type operation of the error causes the compiler error, strict data types can prevent some error assignments. Since the VAR keyword is required when using the strict data type definition, the critical data type definition cannot be used for global variables that use _Global modified global variables. Strict data types can define the data type of the Flash built-in class, or you can define the type of data created by the user yourself. Strict data types also involve the problem of compiler compilation, if you use a strict data type to declare or define variables, then the script settings of the document will be ActionScript 2.0. Variables are information storage containers, which requires initialization when defining variables, which is recommended in the first frame. The survival and scope of the object is defined on the timeline. The survival of variables also depends to a certain extent, but the scope of the variable and the survival period will be more subtle in the Flash operating state. The survival of the variable refers to which region variable can be accessed and used. ActionScript 2.0 defines three variable survival: Local variables only exists in the inside of the defined function body; Timeline Varabbles can be accessed by any script on this timeline; global variable (Global Variables) and functions can be accessed by any script of any time axis of the current document. Function in Flash ActionScript is considered class, so it also has a field and survival.
• Local Variables: Use a VAR declaration in the function when defining a local variable. The local variable is valid within a statement block, and the volume of variables will end at the end of the statement block. Note that if you are a local variable, you must use a VAR declaration, otherwise the system will treat it as a time axis variable. Using local variables in a function is a very good habit, which makes functions run independently.
• Timeline Variables: Timeline variables can be used in any script defined on its timeline. Definitions and initialization time axis variables can be performed in any frame of the time axis, and the variable must be initialized before use, otherwise the variable will not be available. It should be noted that the previously mentioned timeline refreshes the variable on the timeline, which is just the visual object of the time axis in the time axis depth area.
• Global Variables: Global variables can be accessed by any time axis and is valid throughout the document. When using global variables, it is necessary to declare the _Global identifier and do not use VAR. If the local variable is the same name with the global variable, this global variable cannot be accessed within the living period of the local variable, the system will give priority to local variables; if the local variable is the same name with the time axis variable, then the local variable will be This time axis variable cannot be accessed, the system will give priority to local variables.
Third, function (Function)
Function is a statement block that implements a certain function. If a function belongs to an object, then this function is called method. If the function is not to any object, it is called a top-level function (Top-Level Function), and a list of these functions can be found in the Action panel. In fact, the function in Flash is an object, and function is existed as the built-in class of Flash ActionScript. The Function class contains two methods function.apply () and function.call () and a function.Prototype property. Define a function, but in fact, a Function type object is defined whose function name is a reference variable. Therefore, the function can assign a value like a variable. For example, in the first frame is defined as follows: var f: function = new function (); // declaration and create a function object
F = function (a, b): Number // assigns a function of a return value to the Number type to the variable f
{
Return (A B);
}
Trace (f (3, 8)) // function call
And the normal function definition method is:
Function F (A, B): Number // Defines a return value is a function of Number
{
Return (A B);
}
Trace (f (3, 8)) // function call
Because the function is an object, you can operate as an operational variable, as long as the function is actuated to a variable. The variable has a global variable, so the function also has a global function. Use the _global keyword to define the function as a global function. You do not have to use the target path when calling this function. The global function can be defined below:
_Global.myfunction = Function (X) // Declaration and definition of function
{
// statement
}
_Global.myfunction (5); // function call
There is also a certain built-in function in Flash, which utilizes these functions to obtain system-related information, complete specific tasks, such as acquiring the version number of Flash Player and get screen resolution. Every function has its own functional characteristics, and some functions require parameters. If too many parameters are entered when the function is called, the excess parameters will be ignored. If the parameters required for the function are not passed, the parameters will be the type of undefined, which is likely to cause the runtime script error. Using the RETURN statement can return the return value of the function, the return statement blocks the function from continuing and returns a processing result. There are some attention here:
• If the function's return value type is specified, it must be returned to the data consistent with its return value data type.
· If the return value is arbitrary type (ie Void), the return value may not be included.
· If there is no return value of the function, the RETURN statement is optional. If there is no RETURN statement, then the system automatically returns an empty string.
Use the target path to call the function, or even call the function in the movie loaded from the outside. If the _Global keyword is used in the function definition, you can call directly to the target path. However, it should be noted that access must be done after the function is created, otherwise the call is invalid. Also, if the definition function is repeated, the new function definition will replace the original function definition, that is, give functions to a new value.