Flash variables and data types

Source: Internet
Author: User
Tags abstract definition empty null null object object variables square root string format tostring
Variables | data | data type
What is a variable? Presumably it's a container for information (or, more specifically, a pointer to a storage location in the memory space, but it's OK to be able to understand it, so you should remember the abstract definition). As long as you can understand that it can store information and can change the information in a variety of ways, I am not a teacher in the university, all day long know that the students to learn the definition of rote, the actual thing is not, "all for the exam."
There is also a concept: what is called initialization variable? is to give it a valid value at the time of definition to illustrate its content and data type. As in the following example, a string variable is defined:
MyString = "I do not understand what is initialization variable";
It's as simple as this, but there are also questions to be noted:
Variable names must be valid identifiers (for example, you cannot start with numbers and characters that are not allowed).
Variable names cannot be similar or identical to the Actions cript keywords and constants.
The variable name must be unique within its scope (scope) (we will refer to what is the scope below).
Variable scope
That is, the variable is accessible within what range. As with tasks in C&c:renegade, an ID card without security Level 3 will not be able to enter a zone with a secure 3.
There are generally three types of scopes: Timeline (Timeline), partial (local), Global.

--------------------------------------------------------------------------------
For a timeline range variable, just like the example above, use = assign and declare, of course you can also use Set action (Flash 4, not recommended, unless you think the code is not long enough):
Set (MyString, "I don't know what initialization variable");
When a timeline range variable is declared, it is accessible in the timeline that declares its entire hierarchy (level).

--------------------------------------------------------------------------------
What is a local variable? I also often ask myself. A local variable is a variable that is accessible in the statement block that declares it (for example, a function body) and is usually used to avoid conflicts and save memory usage.
Declare that it can use the var keyword:
function Localvar () {
var mylocalstring = "This is a local string variable";
Trace ("Inside:" + mylocalstring);
}
Localvar ();
Trace ("Outside:" + mylocalstring);
The above mylocalstring string variable is automatically cleared out of memory at the end of the function. So the trace statement outside returns the result to null ("Outside:"), and trace in the function body returns: "Inside: This is a local string variable".
If you delete the var keyword, then mylocalstring becomes a variable in the timeline range. After the run, Inside and Outside are shown as "This is a local string variable." We'll also mention the contents of the local variables in the following function chapter.
Maybe you noticed that there's a trace action in the function. It can send the value of the specified variable to the Output window in test mode (testing Movie, Ctrl+enter key) to check that the code that handles the variable is normal at run time. This is a very convenient way to check your code and use it frequently.
If you encounter unknown functions and keywords in your code, you can use the Action Panel's Reference button (Reference) to open the reference panel to display the cursor location keyword or the built-in function as well as the object details, or you can open it by combining the key shift+f1. The following is a reference panel:
Global variables, as the name suggests, are variables that can be accessed throughout the Movie. Its declaration is more special:
_global. Myglobalstring = "This is a global string variable";
The declaration uses a _global identifier and a dot syntax (dot Syntax, which is discussed later in the section). The _global identifier is added in Flash 6 to create global variables, functions, objects, and classes (class, and it is not clear what classes are, you can look at C + + books).
Generally speaking, _global is a parallel object with the lowest _level0. All global variables, functions, objects are actually equivalent to its properties, methods, and child objects. About its specific content can consult the Help file, generally we just know how to use on the line.
Global variables are masked by a timeline variable and a local variable of the same name (obscured), and we're going to access it in other ways. Take a look at the following example:
Defining Global Variables
_global.x = "global";
A simple function
Function Show () {
Trace ("Within the function when there is no local variable:" +x);
var x = "local";
Trace ("Within the function of a local variable:" +x);
Trace ("Call a timeline variable when there are local variables:" +this.x);
}
Test
Trace ("------------when no timeline range variable)";
Show ();
Trace ("Direct call:" +x);
Setting timeline variables
x = "Timeline";
Test
Trace (-------when-------has a timeline range variable);
Show ();
Trace ("Direct call:" +x);
Trace ("Call global variable when there is a timeline range variable:" +_global.x);
The result of the example above shows that the global variable should be called with the _global. Variable name when there are variables with the same name as the global variable in the current timeline range, and the variable name can only be accessed directly from a variable of the same name in the timeline range. The same local variable also masks the timeline variable (you can use this. Variable name to call the timeline variable) and global variables (then you can call global variables with the _global. Variable name).
Using variables in your code
Using variables is simple, and a bit of programming-based readers can do it. Here is an example to illustrate the use of variables in the Actions cript:
function VARs (x) {
if (x<5) {//if x is less than 1
x = x + 1; X plus 1
} else {
x = 10; Otherwise, the value is assigned 10.
}
Trace ("x=" +x);
return x;
}
VARs (2);
VARs (6);
Trace ("x=" +x); Test if x is a local variable
An argument in a function declaration is also automatically used as a local variable, such as an x in the example above, without the Var declaration. It can be seen from the last sentence of the above example (the return value is empty).
We can see that there are 4 actions in the function defined above for the local variable x (declared in the function argument):
Assignment: is to change the contents of the variables stored.
Operations: Use operator operations and return results. The x = x + 1 In the example above can be treated as an operational operation (x + 1) and an assignment operation (x = ...). Compound statement. For a logical operation (the condition of the If statement above), returns a logical value of TRUE or false (just as x<5 returns True when X is 4).
Function and command invocation: As above trace ("x=" +x); , X is passed as an argument to a function, which represents the actual content it stores (same in return x).
There is a lot of flexibility in the use of variables in the Actions cript, and we'll explain them for different data types.
Common data Types---strings | String
A string is a series of characters, for example, "This" is a string.
Defining a String variable is simple, as long as you assign a string data to it when you initialize it:
Chapter = "2nd chapter";
Section = "2nd quarter";
Section_name = "Common data type";
full_name = section + "" "Add section_name + 999; Connection string
if (typeof (Full_name) ne "string") {
Full_name = "wrong type!";
}
Trace ("full_name=" + full_name);
The value of the Full_name in line 4th above is the result of the preceding two variables (section and section_name) and a constant (999) (using the + and add operators, whose functionality is the same). Note that the last numeric constant of this line of code, 999, is not the same type of data, if this line of code is wrong in a language where data type checking is extremely strict in Pascal. However, the Actions Cript can automatically convert it to string format without the need for specialized functions (of course, the safest method is to use the toString () function or string () function of the number object). This shows that the Actions cript is a weak type of check language (that is, not strictly restrict the operation and transfer between the various data types), which is somewhat similar to VB, but a little more.
In the end I used an if statement to test whether the code followed by 999 is working properly (that is, checking that the value returned by the expression is a string, although this is not necessary in practice). Note that I am using the logical operator NE for the string type, of course this is only to show the specificity of the string data type, the actual application of!= also can be. For the typeof operator, we will explain in detail at the end of this chapter the .<!--/message--><!--Sig-->
<!--/icon and title--><!--message-->---------------------------------------------------------------- ----------------
In practice, there are special characters that cannot be entered directly into a string, such as the inability to enter quotes directly in a string (which can break the integrity of the string). Then we need to use the escape character (escaping). To use the escape character, first enter a backslash (\), and then enter the appropriate code. The detailed code list is as follows:
Escape characters represent characters
\b Retreat characters (ASCII 8)
\f Page Feed (ASCII 12)
\ n line feed (ASCII 10)
\ r Carriage return (ASCII 13)
\ t Tab (ASCII 9)
\ "Double quote character
\ ' Single quote character
\ Slash backslash character
\000-\ 3,778 characters in binary notation
\x00-\xff hexadecimal representation of characters
\u0000-\UFFFF hexadecimal 16-bit Unicode character

For example:
Trace ("He said:\" I don\ ' t care about you.\ "\nand she Smiled:\" really?\ ");
You can read the actual content within the string of the above code according to the control list above. The output after the run is:
He said: "I don ' t care about you."
And she smiled: "Really?"
As you can see, the escape characters are converted to the corresponding actual characters. This is the role of the escape character, the reader of C + + should be very familiar with.
Value | Number
The numeric data in the Actions Cript is a double-precision floating-point number (it doesn't matter what it means, but it's OK to know it's a numerical value, it's just a range limit).
You can perform any appropriate action on numeric data. The following example:
A = 1;
b = 2;
sum = a + b; Find A, B's and
if (sum>0) {//If the result is greater than 0
Square_root = math.sqrt (sum); Use the square root function of the Math object to find the square root of sum
}
Trace ("sum=" + sum);
Trace ("square_root=" + square_root);
Finish.
Logical Variables | Boolean
A logical variable is also called a Boolean variable (from its English name). It has only two values: True and False. If necessary, the Actions Cript automatically converts its value to 1 and 0, and you can assign it a value of 1 and 0 (this is probably the result of compatibility with Windows API function calls).
A = 10;
B1 = 1;
B2 = false;
if (B1 = = True) {
A = a + B1;
} else {
b2 =!b2;
}
Trace ("a=" + a);
Trace ("b1=" + B1);
Trace ("b2=" + B2);
The code above mixes the operations of numeric and logical variables. A = a + B1 adds the logical value B1 (true 1) to a, B2 =!b2 is the inverse of the B2 (that is, either false to True or false from true, because there are only two cases of logical values: TRUE or FALSE). You can try to change the value of B1 to see the different effects.
Objects | Object
An object is an important data type in the Actions cript. More clearly, it is the class in most programming languages now. In fact, the use of MovieClip in Flash is also a predefined class, but it is somewhat special.
About objects we'll detail in later chapters
Film | Movieclip
A movie is the only data type in Flash that refers to an image element. You can manipulate its instance (Instance) by MovieClip the various properties and methods of the object.
We will also explain the film in detail later.
Empty | Null
Empty? I mean "empty", which means nothing. This is what this data type means, and it has only one value: null. So what's the use of it?
Used to indicate that a variable has not been assigned a value
Used to indicate that a variable already contains no data
Used to indicate that a function has no return value
One of the arguments used to represent a function is omitted.
Don't think it's meaningless, it's very useful when it comes to specific procedural questions.
Definition | Undefined
Undefined type is similar to Null and has only one value: undefined.
It is used to indicate that a variable has not been assigned a value.
typeof operator

In practical applications, we often encounter situations where we need to judge the data types of specific variables and objects. Flash provides a very useful typeof to solve this problem. Especially for the design of custom functions, determining the type of parameters is a very important step.
Parameter type return value (String)
String string
MovieClip MovieClip
Button Object
TextField Object
Number number
Boolean Boolean
Object Object
function function
Undefined Undefined
NULL NULL

The typeof operator is highly prioritized and can be calculated before a logical operation or an arithmetic operator. The following are examples of its specific application:
Types of constants
Trace ("Type of numeric constant 36:" +typeof 26);
Trace ("Type of string constant what:" +typeof "what");
The general typeof operator can be used like the +,-, add, and so on.
But in order to avoid mistakes, it is recommended that you use parentheses, as in the following example
Trace ("Type of logical constant true:" +typeof (true));
Type of Object
Trace (Object () Type: +typeof (object ());
Trace (the type of "new string ():" +typeof new String ()); Note that the new operator has higher precedence than typeof
The type of the method of a function depends on its return value
Trace (the type of the "Math.sqrt () Method:" +typeof math.sqrt ());
Trace (the type of the "Math.tostring () Method:" +typeof math.tostring ());
Null NULL type
Trace ("Type of NULL:" +typeof null);
Here I used a multilevel typeof to see the type of value returned by typeof
Trace ("typeof type of Return value:" +typeof (typeof null));
You can also try to change the code yourself to see what the typeof of other things are.
This is the end of the chapter, the next chapter will be more realistic (because some people say that these things are not necessary to speak, unrealistic). The object aspect of the data type is described in the object section
Source: Flash Bar


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.