Part 1: ActionScript foundation
Chapter 1: Code Format and Specification
Code format
Each of the ActionScript is ended in a semicolon ";" end. Different from the Basic language, the ActionScript statement allows you to write a multi-line writing like C , Java, and Pascal, that is, allowing a very long statement to be divided into two or more code. OK, as long as there is a semicolon at the end. The only disadvantage that allows the statement branch writing is (at least for many familiar Basic people): The statement does not forget the semicolon. The unique restriction of statement branch is a string cannot be cross-line However, the two sections must be in the same line.
Branch writing long statement is also a good typological method, such as the following statement segment (what is the meaning of the following statement below, is a comment behind the double slam):
duplicateMovieClip ( "MyOldMovieClip", // old MovieClip instance name "MyNewMovieClip", 999); // instance a new copy of the name and location of the level setProperty ( "MyNewMovieClipNameIsNewMC", // you want to modify the properties of the MovieClip instance name _alpha, "30" ); // Modify its transparency of 30% but seems to be automatically formatted in Flash MX is not very complete. The above code can run, but it will be erroneous when using automatic formatting. The problem is inserting a comment statement between it. After all, the example I wrote is not very good to comply with the Flash MX's grammar specification (but according to the syntax of the C language is absolutely no problem), so remember: Only in the case of the statement or very complicated Use multiple lines to write. -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------
A good programmer will know how to join the comment in the program code. Maybe you will say: It doesn't matter, I know what I wrote. But if you read this code after 1 month? It is obvious, you Can't remember. So what happened to play a few words?
Add a comment to the ActionScript code to use the following format:
STATEMENTS; // This is a single-line annotation, starting from the double slash, the back part of this line is an comment content statements; / * This is also a comment, but it can be wrapped * / is as simple (not useful). note / * And * / are not recommended (because it must be paired, it will often cause errors).
-------------------------------------------------- ------------------------------ In addition to comments, another way to increase the code readability is to use the code version format. Below is A section with high readability:
/ / Calculate the function function function function function function function function function f (x) {// if x is less than or equal to 0 retURN 1; // Return 1} else {RETURN X * f (x-1); // Otherwise return to the result}}}
The previous version of Flash MX must be arranged by the programmer itself, and the Flash MX's ACTION panel has more auto format buttons, which can be used to format the code (also grammar check, quite convenient) The formatted option can be set in the dialog box corresponding to the Auto Format Options item of the ACTION panel pop-up menu. Syntax Highlighting is the functionality of many programming language IDE (integrated development environment). Flash also has this feature. The color of the syntax coloring can be set in the dialog box corresponding to the Preferences item of the ACTION panel pop-up menu.
In this regard, there is nothing to explain. Just hope that everyone can use grammar to discover errors and read code.
Flash MX's new code prompt (Code Hint is not unfamiliar for readers familiar with Visual Studio 6.0 version. For example, enter "." After entering an object name, you will display the relevant properties and method lists. After entering the function name, "(", you will display the relevant function format. Specific you can experience yourself when you enter the code. The syntax specification keyword keyword is a basic constructive unit of any language program. It is a program Reserved Words (Reserved Words) cannot be used as other purposes (cannot be used as custom variables, functions, object names).
Flash's ActionScript keyword is not a lot, below their list:
Flash MX ActionScript Keyword BREAK Take out the Class Case (CASE) Class Case Define a Switch statement Condition Select Sector Document NEW Using Constructor Creating a New Object Continue Jump to the next item of the cycler Return returns a value in a function DEFAULT Definition Switch statement Switch Switch Defines a Multi-Category Selection Text DELETE Clear The Memory Resource Use of the Specify Object This This reference The current code is located in the ELSE definition if statement returns a statement block TypeOf Object Type FOR Defines a loop VAR Declaring a Local Variable (Local Variable) Function Defines a Function Block Void Declaration Return Value Type Uncertain IF Define A conditional Bill Block While Defines a Condition Circulation Sector In an object or an array of elements Create a loop with define a statement block that operates to the specified object
These are not asking you to take them down like a back word (this is why China's quality of education is so bad), just hope that everyone has some words in Flash, and some words cannot be used freely. This is useful when analyzing errors (especially for beginners).
Size-to-write sensitive is like C and Java, ActionScript is case sensitive.
This means that if IF is not equal to IF. If I use IF in the code, an error will occur when running and checking. Avoiding this situation is actually very simple: pay more attention to whether the input code is automatically colored SYNTAX HIGHTLIGHTING.
But for variables, instance names and frame labels, ActionScript is not case sensitive. Despite this, I still recommend that you keep the case when writing code. This is a good habit.
In addition to keywords, the most important part of the program language is the operator. Don't say that I am dead, because you don't understand these things, you can't do anything.
Below is a list of operators, the priority of the operator (ie, when several operators appear in the same expression, which one is active first) from top to date:
Operator Description One Dollar Plus - One Dollar (Unary) Logic Non! Logic NOT NOT NOTE (Flash 4 Format) Post-Summit - Later (POST) Decreasing () function calls [] array (array) element. Structure (Structure) Member Preliminary (PRE) Equation - Influence (PRE) Decrement New Creating Object Delete Delete Object TypeOf Get Object Type VOID Return Undefined Values * Multiply / In addition to the first mode (division of the restraint) add the ADD string (in the past &) - Decrease << Position left shift >> Press the right shift >>> Press to right (no symbol unsigned , Filled with 0)
Especially like?: This type of extremely simplified operator, beginners are more difficult to read. For example, the following code:
X = 5; y = 10; z = (x <6)? x: y; // If x is less than 6, the value of X is assigen to Z, otherwise the value of Y is ZTRACE (Z); // Returns 5 From the previous example, all sample code in this tutorial can copy the first frame of the newly created Flash animation. Then pass the Test Movie item or press the combination key Ctrl Enter through the composite key ctrl enter. So you can see what the actual effect is there. Of course, you can also try to change these code, understand the content of teaching through changes.
It is better to understand the code below (even if it is very redundant):
X = 5; y = 10; if (x <6) {// If x is less than 6, the value of X is assigen to Z = x = x;} else {z = Y; } Trace (z); // Returns 5 constant constants Will not change in the program operation.
For example, values 1, 2, 3 ..., logic value true, false, and more. There are also some system built-in constants, which can be seen for the content of the help file.
The most common statement of expression in ActionScript is expressions, which are usually composed of variable names, operators, and constants. Below is a simple expression:
X = 0; the left side is the variable name (x), the middle is an operator (assignment operator "="), the right side is constant (value 0). This is a very simple assignment expression. Benevan to declare this expression. (DECLE) A variable is prepared for the next step. The expression is divided into assignment expressions, arithmetic expressions, and logical expressions.
The assignment expression has been explained, it is a value for the variable. The arithmetic expression is the expression of mathematical operations, such as: 1 3 (return value is value 4). Logical expression is the expression of logical operations, For example: 1> 3. Only logical expression returns a logical value. The front 1> 3 return value is false, ie 1 greater than 3 is false.
The multiple expressions can be combined to form a composite expression, which generally use this expression. For example:
T = 3 * 3 (2 3); x = 1> 3; the second line above is a logic-assignment composite expression. First, Flash calculates the value of logical expression 1> 3, and then it The value is assigned to X (ie x = false;). For the composite expression, the order of operations can be referred to the above operator table. To change the order of operation, the cracker can be used (the first line of example), which is consistent with other languages. .
This chapter will so far. In the next chapter, we will discuss the use of data types and variables in Flash.
Chapter 2: Variables and Data Type Variables Define Variable Variables? Probably, it is a container that stores information (more precisely means pointers in a storage location in memory space, but can be understood, why bother to remember Such an abstract definition). As long as it can be understood that the information can be stored and the information can be changed through various ways, I am not a teacher in the university. I know that the students die hard back definition, the actual thing is a little bit No, "everything for the exam". There is also a concept: What is the initialization variable? It is a valid value to indicate its content and data type when defined. Just as the following example, a string is defined (String )variable:
MyString = "I don't know what is initialization variable"; it is so simple, but there is also a matter of attention:
The variable name must be a valid identifier (for example, the character cannot be started). The variable name cannot be similar or identical to the ActionScript keyword and constant. Variable names must be unique in its scope (In the following we will mention what is a role). The variable scope is also a variable can be accessed. Just like the task in C & C: Renegade, the ID Card without Security Level 3 will not be secure Area of level 3.
The scope is generally three types: timeline, local (LOCAL), Global.
The variables in the timeline range are like the above examples, and the value is used and declared, of course, you can also use SET action (flash 4, not recommended method, unless you feel that the code is not long):
Set (MyString, "I don't know what is initialization variable"); Timeline range variable declaration, it is accessible to the time line of declaring its overall level (Level).
The local variable is to declare its statement block (eg, a function body) is accessible variable, usually used to avoid conflicts and save memory occupancy.
Declare it can use var keywords:
Function Localvar () {var myLocalstring = "This is a local string variable"; Trace ("INSIDE:" MYLOCALSTRING);}
Localvar (); Trace ("Outside:" MyLocalstring); The above MYLOCALSTRING string variable will automatically clear the memory at the end of the function. So the time outside the TRACE statement returns the result is empty ("Outside:"), TRACE in the body returns: "Inside: This is a local string variable."
If you delete the VAR keyword, MyLocalstring is a variable of the timeline. After running, INSIDE and OUTSIDE are "this is a local string variable". In the back function, we will also mention local The contents of the variable. Maybe you noticed that there is a trace action in the function. It can send the value of the specified variable to the output window in the test mode (Testing Movie, Ctrl Enter Competence) to check at the Output Window to check at runtime Whether the code for processing the variable is normal. This is a very convenient check method that is often used in testing code.
In the code, if you encounter an unclear function and keyword, you can open the reference panel to display the cursor location keyword or built-in function and the details of the object via the ACTION panel, or the details of the object can be made. F1 Open it ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------------------------------
Global variables, as the name refers to variables that can be accessed throughout Movie. Its declaration is special:
_Global.myglobalString = "This is a global string variable"; the statement uses a _Global identifier and some title (Dot Syntax, will be discussed later). _global identifier is new in Flash 6, use You can take a look at the C books in creating global variables, functions, objects, and classes (Class, unclear what you are.
In general _global is an object with the bottom _Level0. All global variables, functions, objects are actually equivalent to its properties, methods, child objects. About it's specific content can be reviewed, general us As long as you know how to use it.
The global variable will not be used by the same name of the timeline variable and local variable shield (OBSCURED), that is, it is accessible anywhere in the MOVIE, and is prioritized for timeline variables and local variables.
Using variables in code is simple, it is very simple, and readers who have a bit programming can do. Use an example to explain the use of variables in ActionScript:
Function Vars (x) {if (x <5) {// If x is less than 1 x = x 1; // X plus 1} else {x = 10; / / otherwise assignment 10} Trace ("x =" x); returnix}
Vars (2); VARS (6); trace ("x =" x); // tests whether the parameters in the local variable function declaration does not require a VAR declaration to automatically act as partial variables (such as X) in the above example It can be seen by the last sentence of the above example (the return value is empty).
We can see that 4 operations have been made to local variables x (declared in function parameters) in the functions defined:
Assignment: It is to change the content stored in the variable. Operation: Use the operator to operate and return the result. X = X 1 in the above example can be treated as an arithmetic operation (x 1) and an assignment operation (x = .. Composite statement. For logical operations (condition of the above IF statement), returning to logical TRUE or FALSE (like x <5 will return TRUE). Function and command call: as TRACE ("X =" x); the same, X is passed to the function as a parameter, which represents the actual content it stores (in returnx; in the ActionScript) With great flexibility, we will explain different data types.
Common data type strings | String a string is a series of characters, such as "this" is a string.
Defining a string variable is simple, just assign a string data to it as long as it is initialized:
Chapter = "Chapter 2"; Section = "Section 2"; section_name = "Frequent Data Type"; Full_Name = Section "Add Section_Name 999; // Connecting String IF (TypeOf (Full_name) Ne" string " ) {Full_name = "Type error!";} Trace ("full_name =" full_name); the value of Full_Name of the fourth line above is the previous two variables (section and section_name) and a constant (999) operation result ( Use and add operators, their functions are the same). Note that the value of the last side of this line code 999 is not the same type of data, if this data type is checked in the extremely stringent language It is wrong. But ActionScript can automatically convert it into a string format, without a special function (of course, the safest way is to use the TOString () function of the Number object or a string () function). This shows that ActionScript is a weak type checking language (ie, the operations and delivery between various data types), this is a bit similar to VB, but it is more than a little more.
In the latter, I use an IF statement to test whether the code of 999 is running normally (ie check if the value returned by the expression is a string, although there is not this necessary). Note that I use a string type Dedicated logical operator NE, of course, this is just to express the particularity of the string data type, in practical application! = Okey. For the TypeOf operator, we will explain in this chapter.