Programming basics

Source: Internet
Author: User
Tags constant goto local time

1. The so-called programming is to send a command to the computer, using a paragraph, or a few commands to let the computer do what you want it to do.

2. The so-called object is the object you want it to do things, just like you want Michael to buy cigarettes, Michael is the object, you want Lily to buy wine, and Lily is the object. in flash, it can be mc, button, or something.

3. A property is a feature and a property. for example, the cigarettes bought by Michael Jacob are made up of three or five cigarettes and packed with ten cigarettes. three or five cards, ten of which are smoke properties. the wine you bought is a pot of wine. erguotou is an attribute. quality and weight are Attributes. In flash, there are many attributes, such as transparency, rotation angle, x, y coordinate values, and so on.

4. The so-called method is something that can be done. For example, Michael Jacob is a sister-in-law. If she wants to buy a car, buy a house, and marry her, then buy a car. li Si is a lame man. If you want him to race, this is impossible. It is no way. what you can do is just a solution!

5. An instance is a copy of an object, all the properties and methods of an object. all instances have all attributes and methods of the prototype object, and the object methods can also be used. like John, his son can marry a daughter-in-law and work like John. the son of a mouse can steal things and grind teeth like his father.

6. The so-called function. the command is organized to enable them to complete a certain function and give a name for this set of commands. When this function is used later, you can directly use a command to tell the set that it is OK. Such a command set is a function.

7. The so-called syntax uses "." to connect an object or instance to its attributes during programming.

2. Now let's talk about the function.

Functions are classified into "pre-defined functions" and "user-defined functions ".

As the name suggests, "predefined functions" are defined internally in flash. They are fixed and cannot be changed. We only need to use them directly.

Now, let's talk about it later.

As the name suggests, "user-defined functions" are our own defined functions. They are very flexible and flexible.

It is similar to the predefined function, just like the name of Lao Tzu and the name of his son. When I was not born

I have already defined it. It is defined by Lao Tzu's Lao Tzu. I cannot change it. (this is like an as developer). When I get married and have a son,

The name of my son must be retrieved by us. If we want to call him a fat man, we will call him a fat man. If we want to call him a dog, we will call him a dog, and a dog, we will call him a dog.

Function ).

The keyword "function" is used to define a user-defined function. (you can think of a fortune teller called function.

Test the eight characters for my son. The syntax is as follows:

Function name (parameter 1, parameter 2 ......)

The name is the function name, which will be uniquely identified in the future. Each function must be named in the same video clip

The only one is to give a name to my son. In the future, it will take a lifetime for my son. If I have a child and a girl, I cannot give them a name.

Name, so we can't call them ).

A parameter is a variable to be passed to a function, for example, goAndPlay (parameter 1). Parameter 1 is a variable. When you assign a value to it, it is

To pass to the function, and then execute the function command, for example, jump to 20th frame, goAndPlay (20), 20 is the variable.

A function can also have a return value or no value. The so-called return value is to pass the changed variable value after the function is executed.

To use other functions, if you want to use the return value, you must first determine that the passed variable must be global.

Variables: global variables, timeline variables, and local variables. Let's talk about the declaration of variables first.

Syntax of the returned value:

Return variable name (or expression); add this statement when defining a function to pass the value of the variable.

Variable Declaration

Local variables are available in their own code blocks (defined by braces.

Timeline variables are variables that can be used in any timeline, with the condition that the target path is used.

Global variables are variables that can be used in any timeline (even if the target path is not used ).

To declare a timeline variable, you can use the set variable action or The assign value (=) operator. These two methods will produce the same

Sample results.

To declare a global variable, you can use the _ global identifier before the variable name. The following example creates the global variable myName:

_ Global. myName = "0312 ";

To declare a local variable, you can use the var statement within the function body. The scope of use of local variables is limited to its code.

It will expire at the end of the code block. Local variables that are not declared in the code block will arrive at the end of its script

Period.

Local variables can also prevent name conflicts, which may cause video errors. For example

As a local variable, you can use it to store the user name in one environment, and store video editing instances in other environments;

Because these variables run in different scopes, they do not conflict.

Using local variables in the function body is a good habit, so that the function can act as an independent code. Local variable

It can only be changed in its own code block. If the expression in the function uses a global variable

You can also change the value of this function.

These are the basis and must be kept in mind. For example, global variables are like bottled wine and local variables.

It is like a casual liquor. Bottled wine can be sold all over the country because it is a brand wine from an authentic manufacturer, such as 38 RMB or 38 RMB.

It's a variable value, but it's too expensive. Why don't I sell 37 RMB 5? People say that I can earn you 2 cents and sell you! In this way, the bottled wine is also

It is bottled wine, but the price has changed because it is variable and its value can be changed.

The casual liquor is different. It is made in our own house and can only be sold in the village of the villagers.

For example, if an elder brother-in-law wants to buy his wine, he must make it cheaper. Otherwise, he will go back to his elder sister's house and will not cook for him.

Also changed, and the local variable value also changed,The value of the variable can be changed!


Programming is the same. The difference is the syntax and writing rules. Let's talk about three basic structures of programming today.Sequence structure, select structure, and cycle structure. If you can understand these three structures, you can go beyond the basics of popular programming languages, such as, c, vb, java, c ++, c #, and asp, just learning their syntax and algorithms and a large number of library functions. Saves a lot of time.
1. Ordered structure statement.
A sequential statement is a statement that is executed one by one sequentially. The next statement can be executed only after the previous statement is executed, as shown in the following statement:
A = 12;
B = 13;
C =;
A = B;
B = c;
This program swaps the values of a and B. The program starts to run from the first statement until the last statement.
Sequential statements are relatively simple, that is, they are executed from top to bottom.
2. Select the structure statement.
Select a statement to test the condition expression first, and then execute different operations based on the test expression results. Select the structure and use the most common if statement. The most common form of if statement is:
If (relational expression) statement 1 else statement 2
The if statement is used to determine whether the given condition is met. One of the two operations is performed based on the result (true or false).The. if statement has three forms:
1,If (expression) statementIf the expression value is true, execute the statement. Otherwise, do nothing. See figure ()
2,If (expression) statement 1 else statement 2If the expression value is true, statement 1 is executed. If the expression value is false, statement 2 is executed. See figure (B)
3,If (expression 1) statement 1
Else if (expression 2) statement 2
Else if (Expression 3) statement 3
:
:
:
Else if (expression n) statement n
Else statement m// This is complicated, but I believe everyone can understand it. It means that if the value of expression 1 is true (this is a branch), Statement 1 is executed, if the value of expression 1 is false (this is a branch), execute if (expression 2) statement 2, that is, if the value is false, then use the selection statement in the false branch, then divide the selection conditions. there are actually many if else statements! Figure (c)









In the above example, we write the else in the same column with the first if (outer if). We hope the else will be paired with the first if, but in fact the else is paired with the second if, because they are closest to each other. therefore, it is best to use the embedded if statement to include the else part, even if it does not do anything. in this way, the number of if and else is the same, from the inner layer to the outer layer, so as to avoid errors. if the number of if and else is different, in order to implement the attempt of the program designer, brackets {} can be added to determine the matching relationship.
If ()
{If () statement 1}
Else
{If () statement 2
Else statement 3}





Now let's give an example of the selection statement. Let's talk about if... else first.

Last night, I discussed it with my wife in bed. We had a wrist. I won my wife and kissed me. My wife won.

Now, let me kiss my wife. This is the selection statement,

If (If) I won.(Expression)My wife kissed me (Statement 1), If I lose (Else), Kiss my wife (Statement 2). Do you understand?

I went shopping today and saw a meal and asked for money. If I had money, I could make two choices,

Give him 5 hairs and 2. If you are in a bad mood and lose 500 yuan in mahjong, you still need to buy food. Don't give it! If I do not have

Money, I can also make two choices. 1. I am a good person. If I can't help him, let's go. 2. I am the wicked. I am with him.

Ask for money. He has a rough invoice in his hand! Richer than me! This programming process is nested with if. Let's look at the program.

How should we implement it:

If (If) I have money(Expression 1 ){
If (If) I'm in a good mood.(Expression 2)Give him five hairs (Statement 1);
I'm in a bad mood (Else) Not (Statement 2);}
I have no money (Else) If (If) I am a good guy.(Expression 2 ){Leave (Statement 3);
I'm not a good guy (Else) Grab the money! (Statement 4 );}
This is the nesting of the if statement. Do you understand this?


[B] [original] basic programming explanation (3: switch application) [/B]

Except for if ..... besides the else statement, the switch statement can also be used for selection. the switch statement is a multi-branch selection statement. if statements have only two branches to choose from. In fact, we often need to select multiple branches. for example, if the score of A student is 90 or higher, the score of 80 or more is B, and the score of 70 or more is C. of course, nested if statements can also be implemented, but it is too troublesome. if statements have many layers of nesting, and the program is lengthy and hard to understand.
As provides the switch statement to solve this problem. The general form is as follows:
Switch (expression)
{Case constant expression 1: Statement 1;
Case constant expression 2: Statement 2;
Case constant expression n: Statement n;
Default: Statement n + 1;
}
Description: 1) the expression behind the switch, which can be of any type (integer, string, etc)
2) when the expression value is equal to the value of the constant expression after a case in the curly brackets, the statement after the case is executed, if the value of the constant expression after all cases does not match the value of the expression after switch, execute the statement after default.
3) the values of constant expressions after each case must be different. Otherwise, contradictions may occur (there are two or more execution schemes for the same value of the expression ).
4) the order of case and default does not affect the execution result. For example, if default appears first, then case appears.
5) after a case branch is executed, the process control is transferred to the next case for further execution. The case constant expression only serves as the statement label and does not perform condition judgment here, when executing the switch statement, find the matched entry label based on the expression value after the switch, and then execute the label without further judgment.
Therefore, after executing a case branch, let the process jump out, switch structure, that is, stop the execution of the switch statement. You can use the break statement to achieve this purpose:
Switch (expression)
{Case constant expression 1: Statement 1; break;
Case constant expression 2: Statement 2; break;
Case constant expression n: Statement n; break;
Default: Statement n + 1;
}
The default statement of the last process cannot be used with the break statement.
Although there is more than one execution statement after case, it does not need to be enclosed in curly brackets and will automatically execute all statements after case. Of course, it is better to add curly brackets.
6) multiple cases can share a group of execution programs:
:
:
Case 'a ':
Case 'B ':
Case 'C': Stop ();
:
When the expression value after the switch is ABC, the Stop statement is executed.
Let's talk about these courses first, and explain them in detail later. Let's see if you can understand them.
Now we use an instance to describe the switch statement. The following program shows the day of the week.
Create a new file, write a dynamic text, and name day.



Create a layer and add the following statement to the frame:
Mydate = new Date (); // create a new Date object.
A = mydate. getDay (); // Obtain the local date of the week and assign it to variable.
Switch (){
Case 0: // if the value of a is 0, run the statement day = "Sunday".
Day = "Sunday"; structure.
Break;
Case 1: // The same principle!
Day = "Monday ";
Break;
Case 2:
Day = "Tuesday ";
Break;
Case 3:
Day = "Wednesday ";
Break;
Case 4:
Day = "Thursday ";
Break;
Case 5:
Day = "Friday ";
Break;
Case 6:
Day = "Saturday ";
Break;
}Test it!




During the test, the dynamic text is displayed as Monday, date. the getday () function returns the value of the week in the specified Date object according to the local time (0 indicates Sunday, 1 indicates Monday, and so on ).
This is the application of the switch statement. Do you understand it?


[B] [original] Programming Basics (4: loop statements) [/B]

The purpose of the loop structure is to reduce repeated code and reduce the burden on programmers. There are three forms: for loop, while loop, and do-while loop.
Here we will also talk about the goto statement.
In as, the goto statement has been converted into a very simple and visualized one. when programmers program in advanced languages, they are very abstract. goto statements are divided into gotoAndPlay and gotoAndStop in. the following parameters are frame labels or frames. here we are talking about programming, rather than simply referring to as. So let's talk about the use of goto in vc and vb. (If you do not like these friends, you can skip them)

InC languageIs an unconditional exclusive statement, the general form is:
Goto statement label;
The statement label is represented by an identifier. The naming rule is the same as the variable name, which consists of letters, numbers, and underscores. The first character must be a letter or underscore. An integer cannot be used as the label.
InVisual basic (vb)Divide the goto statement into the fourth structure, that is, the redirection structure. Syntax format: goto {row/row label}
The line label must start with an English letter and end with:. The line number is not followed by a number:
The function of the goto statement in C and vb is the same,When the program is running, once a Goto statement is encountered, it is transferred to the row number or line label, and the corresponding program is executed at the statement label.
Note:The structured program design method restricts the use of the goto statement, because misuse of the goto statement will make the program flow irregular, the readability is poor, but it does not absolutely prohibit the use of the goto statement. Generally, there are two purposes:
1) the statement and the if statement form a loop structure.
2) jump from the circulatory body to the external body of the circulating body.C languageThe break statement and the continue statement can be used to exit the current layer loop and end the current layer loop. The use of the goto statement is greatly reduced, the goto statement is used only when the inner loop of a multi-tier loop jumps out of the outer loop. however, this usage is not in line with the structured principles and is generally not suitable for use. It is used only when it is a last resort or greatly increases program efficiency.

Introduction:
The basic format of the for loop is
For (expression 1; expression 2; Expression 3) {statement Group}
The statement group in braces can also be a statement.
The execution process is as follows:
1) first solve the expression 1.
2) solution expression 2. If the value is true (the value is not 0), execute the embedded statement (statement group) specified in the for statement, and then execute step 3rd. if the value is false (the value is 0), the cycle ends and the process is forwarded to step 5th.
3) solution expression 3
4) switch back to Step 1 to continue execution (one loop)
5) when the loop ends, execute the following statements of the for statement.
The simplest form of for statement is as follows:
For statementSee figure!



Explanation:
I have 100 yuan on my body. I have a high smoking addiction. I have to smoke a box every day. So I ask Tom to buy a box of Dahong River and 5 yuan each day. but when I spent money, I could not ask m to buy cigarettes. I had to smoke tobacco from my house. so a loop structure begins.
For(I have 100 ,"Expression 1", I can only buy cigarettes if I have money, so I have more than five yuan to buy cigarettes ,"Expression 2"Every time I buy cigarettes, I have to pay less than five yuan ,"Expression 3"){
Ask Tom to buy cigarettes ,"Statement".}
I have no money, so I have to smoke tobacco"For statement"
The order is as follows:
1) money on me=100;
2)100> 5So I can buy a box of Big Red River.
3)Execution statement"Ask Tom to buy cigarettes"
4) after I bought a cigarette, I lost 5 RMB in money.Money on me = money on me (100)-5 "expression 3"(95 more left)
5)95> 5So I can also buy a box of Big Red River.Execution statement"Ask Tom to buy cigarettes ". I have 90 cigarettes left after I buy them. I can still buy more than 5 cigarettes. it wasn't until I asked m to buy cigarettes for 20th times,The remaining 0 blocks are insufficient.Ah, how can I buy cigarettes,The cyclic condition is false.Now! You cannot execute the statement "ask Tom to buy cigarettes".Execute the statement following the for statement"Smoke leaves planted in your own house ".
Do you understand?

For loops are commonly used. Choose to introduce another loop while statement. The while statement is used to implement the "when" loop structure. The general form is as follows:
While (expression) statement
When the expression value is true (not 0), execute the embedded statement of the while statement. The flowchart is shown in Figure B.
Note:
1) If the loop body contains more than one statement, it should be enclosed in curly brackets and appear in the form of a composite statement, that is, the statement group. If no curly brackets are added, the while statement only ranges fromThe first semicolon next to the while statement.As a programmer, you should have a good programming style. Don't think about where to write it. Make the program look neat and orderly and clear at a glance. therefore, we recommend that you use curly braces to develop a good programming style no matter how many statements you have.
2) there should beStatement that causes the loop to end to avoid endless loops.
So what is a statement that causes the loop to end? It is a statement that gradually false the expression value. The loop ends only when the expression value is false .!





After understanding the while statement, do while is simpler.
Do while is characterized by executing the loop body first, and then determining whether the loop condition is true. The general form is:
Do
Loop body statement
While (expression );
It is executed in this way,Execute a specified loop body statement first, and then judge the expression. If the expression value is true, return and re-execute the loop body statement. until the value of the loop condition expression is false, the loop ends.See Figure c!
Generally, when the while statement and the do while statement are used to process the same problem, if the body of the loop is the same, the result is also the same. However,If the expression after while is false at the beginningThe results will be different.
Because the expression value of the while statement is false, you can directly execute the statement after the while statement without executing the loop body statement. while do while executes the loop body statement first, and then checks whether the expression value is 0.In this way :"Whether the expression value is true or false, the do while statement must execute a loop body statement.. "The do while statement is different from other loop statements. You must remember that when you need this situation, you should first remember: do while!




Choose to talk about Loop nesting. it is the same as if nesting. A loop body can contain another complete loop structure, called loop nesting. embedded loops can also be nested.Multi-layer loop.The concept of nesting is the same in various languages!Three types of loops (for, while, do while) can also be nested with each other. As long as you can understand the logical relationship, nested n can be. for example, the following example is consistent with the standard .:
1) while (){
:
While (){:}
}
2) do {
:
Do {....}
}
3) (;;){
:
For (;;){:}
}
4) while (){
Do {.....}
While ();
}
Not much ........


Write the loop structure.
Comparison of several cycles
1. All three cycles can be used to handle the same problem. Generally, they can be replaced by each other. Remember not to advocate goto.

2. The while and Do while loops are designed to specify the loop conditions after the while loop. The loop body should contain the statements that make the loop tend to end (such as ascending and decreasing, I ++, I --).
A for loop can contain operations that make the loop tend to end in Expression 3, or even put all the operations in the loop body in Expression 3. Therefore, the for statement is more powerful, all functions that can be completed using the while loop can be implemented using the for loop.

3) When a while and do while loop is used, the initialization of the loop variable should be completed before while and do while, while the for statement can be implemented in expression 1, the initialization of the variable, is the declaration variable. assign values and so on.

4) while do while for loop, you can use the break statement to exit the loop and use the continue statement to end the loop. The break and continue statements cannot be used for the loop consisting of the if and goto statements.


Break and continue statements
1, break

Do you still remember the switch statement in the second part of the selection statement?
In the switch statement, we have used the break statement to jump out of the switch structure. Continue to execute the next statement. In fact, the break can alsoJump out of the loop bodyTo end the loop in advance. The usage is the same as that in switch: break;
For (I = 1; <10; I ++)
{
A = pi * I; pi indicates the circumference rate.
If (a> 100) break;
}
2, continue
General format: continue;

Its role is: EndThis cycleThat is, skip the unexecuted statements under the loop body, and then judge whether to execute the loop for the next time. (that is, when the loop is executed to continue, the loop is ended immediately, re-determine whether the cyclic condition is true)

Break and continueDifferencesYes:

The continue value ends.This cycleInstead of suspendingEntire cycleAnd the break statement ends.Entire cycleProcess.

[B] [original] basic programming explanation (v: Object) [/B]

In the object-oriented programming, we turn the information organized by groups into classes. We can create multiple instances of a class, which is called objects.
You can create your own class (custom object) or use the built-in action script class (pre-defined object). The built-in class is located in the "object" folder on the "action" panel.
When you define a class, you need to define all attributes (features) and methods (actions) of each object created by the class, just like defining the objects of life. For example, you can say that a person has attributes such as gender, height, and color, as well as methods such as conversation, walking, and throwing. "Person" is a class, and each person is an object or an instance of this class. "James is a one-meter-eight-year-old boy." He has attributes such as gender, height, and other methods to speak. In this example, "Michael" is an object. for another example, we have defined the "talking girl" class, "talking girl", and "talking girl" as an example.

Objects in action scripts can be pure data containers, or they can be graphically represented as video clips, buttons, or text fields on the stage. All video clips are built-in class MovieClip instances, and all buttons are built-in class buttons instances. Each video editing instance contains all attributes of the MovieClip class (for example, _ height, _ rotation, _ totalframes) and all methods (for example, gotoAndPlay, loadMovie, and startDrag ). Now, do you understand? However, these are built-in objects and pre-defined objects.

To define a class, you can create a class namedConstructor. (The built-in class has built-in constructor .) You can generate your own instance like a predefined object. When defining an object, you can also define attribute methods for your own defined object. the defined object is actually to build a function for the defined object. Such a function is called "build function ". you can describe the attributes and methods of an object in the function. after the definition is complete, you can create an instance of the defined object in a future program for future convenience.
The name of the constructed function is usually the same as that of the object,
Syntax:
Function name (parameter ){
This. Parameter =?
:
:
}WhereThe function name is the object name.. The parameter can be interpretedAttributeWhen an object is built, flash automatically generates a keyword"This"To represent this object, so"This. Parameter =?"The parameter attribute of this [color = # D from 143C] object is defined as follows "? "Action or value. "..." Is a ellipsis, indicating that more can be defined.

For example, if you need information about a bicycle rider in your video, you can create a constructor function "bike ride", which has attributes "time" and "distance ", and how to tell you the speed of the bicycle ":

Function bike ride[Function, object name](Min, km[Parameters]){
This. Time = minute[Value or action]
This. Distance = km;
This. Speed = function () {return this. Distance/this. Time ;};[This is a user-defined function. The running result of the function is the return distance/time value.]
}

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.