Flashas Rookie Introductory Tutorials Download 1th/2 page _flash as

Source: Internet
Author: User
Tags numeric value square root
A preliminary study of ActionScript programming


Computer programs are composed of commands, functions, operators, conditions, and loops. A command is a series of instructions for a computer that performs a calculation and return value, a combination of several data in a particular way by an operator, a condition that tests one or more values to return a Boolean value of TRUE or false, and the loop structure allows the program to repeat a series of identical instructions.
A variable is a container for storing data, with variable names and variable values.
Programming is a matter of patience, first you have to understand what kind of problem to use the program to solve, and then you need to break down the problem into several steps, and then divide each step into smaller steps until each step is so small that it can be easily solved.
After the program is written, there may be a lot of vulnerabilities or flaws, so you need to debug the program until the program runs correctly.
3.1 Basic structure of the program
Computer programs are composed of commands, functions, operators, conditions, and loops.
3.1.1 commands, functions, and operators
Before that, we used keywords to describe the elements in ActionScript, such as keyword gotoAndPlay, which is also a command.
A command is an element in ActionScript that tells Flash what to do with a particular action. It is called a command because it will be strictly followed, and if you want to jump to a nonexistent frame with gotoandplay, such a command cannot be executed.
A command is the most basic element in a program, and it is almost impossible to do anything without using a command in Flash. You will learn a lot of commands from this book.
Functions are the elements used in ActionScript to perform calculations and return results. For example, a specific function can compute and return the square root of a specified number.
Both commands and functions can use parameters. A parameter is a value that is passed to a command or function. For example, the gotoAndPlay command requires at least one frame number or frame label as an argument. A function that asks for the square root also needs a numeric value as an argument.
Unlike commands and functions, operators are mostly symbols, not letters. For example, the + operator performs a two-count addition operation.
You'll use a lot of commands, functions, and operators in your ActionScript program.
3.1.2 Variable
Writing complex computer programs often requires a lot of information to be stored. Sometimes you may just need to store a short amount of time, for example, if you need to repeat the same command 10 times, you need to count the number of times the command is executed, until it reaches 10 times.
All programming languages use variables to store information. A variable is composed of two parts: the variable name and the value of the variable.
1. Variable name
A variable name is usually a string of words or words, or it can be a letter. In general, you need to specify a meaningful name for the variable as much as possible.
For example, if you are using a variable to store the user's name, it is a good choice to use username as the variable name. If you use n as a variable name, it seems too short; if you use name, you may be confused with the names of other objects in the movie.
The unwritten specification of assigning variable names to variables in ActionScript is that variable names usually begin with lowercase letters, and when a new word appears, capitalize the first letter of the new word, such as username, A long example is a currentuserfirstname.
Spaces are not allowed in variable names, and special symbols are not allowed, but numbers can be used.
2. Variable type
You can store different types of data with variables. Numbers are the simplest type of variable.
You can store two different types of digits in a variable: integers and floating-point numbers. Integers have no decimal parts, such as 117,-3685 are integers. Floating point numbers have decimal points, such as 0.1, 532.23, and 3.7 are floating-point numbers.
You can also store strings in a variable, which is a sequence of characters, can be one or more characters, and can even have no characters, that is, an empty string.
Use quotation marks to define a string so that it differs from other variables. If 7 is a number, and "7" is a string, this string consists of a character 7.
In other programming languages, you may need to define the specific types of variables to be used in the program at the beginning of the program, but you don't need to declare variables in ActionScript, you just need to use them directly, and flash will automatically create variables for them the first time they encounter them.
In addition, the type of data that a variable can hold is not strictly qualified, a variable can hold a string in one place, and a number in another location.
This flexibility is not always available, but it allows programmers to worry less unnecessarily.
Another problem that ActionScript programmers don't have to worry about is the problem of spatial recycling of discarded variables. That is, when you no longer need to use a variable, you may need to reclaim the storage space that the variable occupies. Most modern computer languages like ActionScript can automatically reclaim space, so you don't have to worry.
There are other variable data types in addition to numbers and string types. For example, an array can hold a series of data rather than a single data.
3.1.3 Conditions
The program itself does not make abstract decisions, but it can fetch data, analyze and compare the data, and then perform different tasks based on the results of the analysis.
For example, you want to check the name entered by the user and make sure it contains at least 3 letters. The thing that the program needs to do is to make a judgment on the username, and if it is 3 or more letters, perform an operation, or another operation if it is less than 3 letters.
Here, it takes two steps to make a decision, the first step is to check if the condition is met, if the name matches 3 letter lengths, if the condition is satisfied, we say the value of the condition is true (true), otherwise the condition is not satisfied, we call the condition's value false (false). All conditions must be one of two values, either true or false. Either True (True) or False (false) data types are called Boolean (Boolean) types.
The second step in the decision is to choose which code to execute based on whether the condition is true or false. Sometimes there is only one option, and when the condition is true, the option is executed, and if the condition is false, no code is executed. There are sometimes two relative options that execute different code when the condition is true and false.
For example, you want the computer to perform 3 different tasks based on a variable's value of 1, 2, or 3, which can be expressed like this:
If the value of the variable equals 1, execute command 1
If the value of the variable equals 2, execute command 2
If the value of the variable equals 3, execute command 3
Conditions are always based on comparisons, you can compare the values of two variables to determine whether they are equal, or whether one is greater than the other or less than the other. If the variable is a string type, you can compare the order in which they are sorted in dictionary order.
3.1.4 Cycle
Unlike people, computers are good for repetitive work. Just repeating a few times can be tiring, but having the computer repeat it for thousands of times is a lot of patience.
Loops are a very important part of each programming language, and ActionScript is no exception. You can specify an instruction to perform a given number of times, or to execute it until the specified condition is met.
In fact, the condition is an important part of the loop, and the entire loop requires only a starting point, an end point, plus a condition that marks the end of the loop.
For example, you need to loop 10 times and use a variable to start counting from 0. Once per cycle, Count plus 1. When the count variable reaches 10, the loop ends and the program continues to execute the part after the loop. The following section represents a standard looping structure:
(1) The previous order of the loop.
(2) The loop begins and the counting variable is 0.
(3) The order in the loop.
(4) Counting variable plus 1.
(5) If the count variable is less than 10, perform the Step (3).
(6) After the cycle of the order.
In the previous step, step (1) is performed only 1 times, step (2) indicates the start of the loop, Steps (3), (4), (5) are executed 10 times, and when the loop is finished, the steps (6) and the subsequent sections are executed.
3.2 ActionScript One corner
When you write your own script, you will use a variety of different keywords and symbols, for you to familiarize yourself with the composition of the script, let's look at a real example.
This is a button-acting script that executes when the user clicks the button (or, exactly, when it loosens the button that is pressed). It does not contain special functions, but it embodies the main structure of ActionScript.
On (release) {
var mynumber = 7;
var myString = "Flash MX ActionScript";
for (var i=0; i<mynumber; i++) {
Trace (i);
if (i + 3 = 8) {
Trace (myString);
}
}
}
The 1th line of the script indicates that the statement in curly braces is executed when the user releases the pressed button. The on (release) structure can only be used for buttons, and several other related uses such as On (press), ON (rollover), on (rollout), ON (DragOver), ON (Dragout), and so on.
The brace {at the end of line 1th represents the beginning of a relatively independent piece of code. The code from {to relative} is a whole, and they are all subordinate to the release event of the button.
Note that the code after the curly braces has a tab character (press TAB) indented on line 1th, each subsequent line of code has the same indentation level until a new curly brace begins, and the statement after the new curly brace adds a tab indent to the preceding statement, and so on, This feature is similar to other programming languages. Flash automatically sets the code you add to the correct indentation style.
Line 1th of the code creates a local variable named MyNumber and sets the value of the variable to 7. The following line of string Flash MX ActionScript is assigned to another variable, mystring. Later, we will introduce two types of variables: local and global variables in more detail.
A semicolon that represents the end of an instruction and should be appended to the end of each complete instruction.
For represents the beginning of a looping structure, where the loop executes 7 (MyNumber) times, where I increments from 0 to 6, and executes the statements in the loop structure every 1 increments. The portion of the for back curly braces is the loop body.
Command trace sends the contents of the parentheses behind it to the Output window. We will describe the output window in detail in the next section.
An if is a conditional structure that tests whether the following content i + 3 = 8 is True, and if true, executes the following statement, otherwise skipping the code snippet.
There is only a trace command in the if structure that sends the value of the variable mystring to the Output window.
The example script ends with three reverse braces}, the 1th represents the end of the IF statement, the 2nd represents the end of the for statement, and the 3rd represents the end of the entire on (press) paragraph.
3.3 Output Window
The Output window is a programming tool that appears only when you test flash movies, and Flash uses it to display error messages or other important information. Users can customize the information to be sent to the Output window using the trace command in ActionScript.
The Output window is useful when you are testing your program. You can use the trace command to display the value of a variable in the Output window, or which part of ActionScript is executing.
The Output window can also help you learn ActionScript. You can write small programs that send information to the Output window, which will help you see the results of the program running.
The best way to familiarize yourself with the Output window is to use it more. Here's how to write a small program that sends the information to the Output window.
(1) Start Flash MX.
(2) Select frame 1th of the timeline to open the action panel on frame 1th.
(3) Use the menu in the upper right corner to switch the action Panel to expert mode.
(4) Click in the script editing area and position the mouse cursor in the script editing area.
(5) In the action panel, enter the following ActionScript:
Trace ("I like actionscript!");
The action panel at this point is shown in Figure 3-1.


Figure 3-1 Entering the trace command in the Actions panel
(6) Press Ctrl+enter button to test the film, because there is no image on the stage, so you will see a blank window, with an Output window, the window shows: I like actionscript! As shown in Figure 3-2.


Figure 3-2 The Output window shows the information in the trace command
As with the action panel, there is also a drop-down menu in the upper-right corner of the output window, as shown in Figure 3-3, which contains commands for copying, cleaning, locating, saving to files, and printing.


Figure 3-3 drop-down menu for Output window
The last command in the Drop-down menu is "debug level," and you can select one of the "errors", "warnings", "details", and if "None" is selected, no information will be displayed.
3.4 ActionScript Basic syntax
The basic structure of the program is described earlier, and the basic syntax in ActionScript is explained below.
3.4.1 Variable
1. Setting variables
The way to use variables in ActionScript is simple, you just need to assign a value to the variable name, for example:
myvariable = 7;
This example sets the value to 7 while creating a variable named myvariable, and you can take a name for the variable without using the myvariable in this example.
You can use the Output window to view the value of a variable, such as adding the following ActionScript to the action panel in the first frame of a blank movie:
x = 7;
Trace (x);
First, the number 7 is stored in the variable x, and the trace command is used to send the value of the variable x to the Output window. When the movie is played, the number 7 is displayed in the Output window.
2. Global variables
Variables can be divided into global variables and local variables depending on the scope of the variable's function.
Global variables are variables that can function at all depth levels of the entire Flash movie. You can set it in a frame and use it in other frames and change its value.
You do not need to use a special method to create a global variable, like the previous example, to set and use it directly, it automatically becomes a global variable.
In many programming languages, global variables can be used everywhere. Flash movies Use a concept called hierarchy (level). The main timeline of the refurbished movie is the root (root) level, and the movie clip is a small movie in the timeline. Graphics and scripts in movie clips are one level lower than the root level. Movie clips cannot directly use global variables in the root hierarchy.
3. Local variables
A local variable can exist only in the current script, but it will no longer exist in other frames. You can create different local variables in different frames using the same variable name, and they will not affect each other.
Local variables can be used to create modular code. When the current script finishes executing, the local variable is removed from memory, and the global variable remains at the end of the movie.
You need to use the keyword var to create a local variable. For example, the following ActionScript creates a local variable mylocalvariable with a value of 15:
mylocalvariable = 15;
When you create a local variable with VAR, you no longer need to use the keyword var in the current code. For example, the following code creates a local variable mylocalvariable with a value of 20, and then changes its value to 8, and then sends it to the Output window.
var mylocalvariable = 20;
Mylocalvariable = 8;
Trace (mylocalvariable);
If there is no special need, try to use local variables.
Current 1/2 page 12 Next read the full text

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.