Flash as Basic Classics tutorial

Source: Internet
Author: User
Tags arrays definition error code eval expression final variables versions
Tutorial

This time to bring you moock in the World Flash Assembly: FF2K1 conference speech, to say the authority of action, of course, the number of Moock, he wrote ASDG (a book) can be compared with the flash as a dictionary of the baby. Although he is such a high master, but his speech is very shallow, if you read somewhere difficult, it must be chocobo translation is not good. (some places are too shallow, I can't stand to change)

The budget for this article is a long speech, so it will be longer to read and you can read it separately.

Also, this article is about FLASH5 as, after all, FLASH4 as has been eliminated.

Chapter One: Starting with the O

To demonstrate, Moock will do a multiple-choice question and answer animation as an example.

This example is implemented in 4 different versions of the method.

For users, 4 versions will make no difference, but for our developers, it's about how to improve the structure and organization of these flash codes.

The focus of improvement is:

* More easy to maintain
* More easy to expand
* More efficient construction

Keep in mind that learning to program is a process rather than an event. Don't worry if you can't follow through when you first learn. Next, Moock said that in order to take care of not much programming experience rookie, will be the most simple start.

About computer languages:

Programming languages are used to send messages to computers, to receive information from computers, and programming languages have glossaries and grammars, such as human language, that we can tell a computer to do through programming languages, or get information from him.

About syntax and logic

The idea of learning programming is more important than learning the grammar of a language. If Flash is a word that understands our language, we say to Flash: "Flash, let a ball play in the screen." Flash will do nothing, Flash wants us to use his world to understand things, such as: Movie clips, buttons, frames, to describe what it wants to do, then we say again: "Flash, let a name Ball_one movie Clip in the screen to bounce, "we have used the MC this flash understand things to describe, but Flash still do nothing, because it needs more information:

* How big is the ball?
* Where does it put it?
* in which direction did it move first?
* How fast it's starting to be
* Does it bounce in the range of the screen?
* How long does it take to bounce?

It seems that we need to have a logical structure to tell flash what it should do

1, a ball refers to a circular MC called Ball_one, 50 pixels in diameter
2, there is also a square MC called square, side length 300 pixels
3, place the ball_one somewhere on square.
4, at a random angle, the speed of 75 pixels per second began to move Ball_one
5, if the Ball_one contact with a side of square, they bounce back
6, keep on moving, know we let it stop

If Flash really understands the words in our language, he should know how to get started.

Summarize the key points:

Whatever language is used, the art of programming ideas lies in how to list logical steps
It would be helpful to describe it in our human language before you begin to do it in a computer language.
Even if you translate them into any computer language, their logical steps should be the same
Computers do not know how to use the assumption to complete your ideas, they do not have the ability to think (Chocobo: is that you want to write out the requirements completely without omission to let it run)

Chapter II: Basic

First learn how to enter programs in Flash
In the Flash menu window, actions can open the action Panel, the action panel to the Right script window, and the tool window on the left.
The Script window is where the script is placed, and the tool window is used to quickly enter various action, operators, functions, attributes, objects
Moock recommended that you use Expert mode, in the Flash menu edit Preferences's actions panel, select Expert mode, each time you open the action pane will automatically set to expert mode (Expert mode can directly enter the code, Beginners will know that a lot of code can not be entered in normal mode.
Some concepts of AS
All code needs to be stored somewhere, it can be frame (frame), button (buttons), or movie clip (movie clip).
As soon as you select a button or MC, the code you enter is stored on top of it, and notice that the action panel prompts for object Actions. You can also store the code in a frame, but at this point the prompt is frame Actions.
When you are still unable to enter the code in the expert mode, you first need to check whether you have selected a frame, button, or MC to store the code.
And then we're like learning all the languages.
Enter in the action panel
var message = "HELLO World";
Trace (message);
And then we press CTRL and enter, and we see the results?
The following two chapters compare the basics. Is Moock a speech fee?

Chapter III: Interpretation

First line of code: var message = "HELLO World";
Tell Flash to remember a container called message (usually in a computer called a variable (variable), in Flash, the variable can be loaded with text, and can be loaded with numbers) containing such data (datum), the content is: "HELLO World"
The "=" equals sign is a commonly used operation symbol (operators), which in Flash does not represent equality, but represents assignment
The meaning of the Var command is to announce to the entire movie what the content of this variable is.
Second line of code: trace (Message);
is to flash display message inside the content, why flash output content, because when you press Ctrl+enter or open the movie online, you enter every action scrpit code, will be through the Flash interpreter ( Interpreter) to interpret and execute
If the interpreter can explain your code, he will run it and return the result you want to return, and if the interpreter doesn't understand your code, he will return the error code--to tell you the reason for the error.
Usually, the commands we send to the interpreter are not only as simple as the command, but also include the contents of the command such as trace (message); The trace output command is the message, which is called a parameter (argument or parameter), and if a command supports multiple parameters, the arguments are generally separated by ",".

The fourth chapter also has some concepts

expression (expression): (2 + 3) * (4/2.5)-1 can be called an expression, "a" + "B" is also an expression that requires an interpreter operation to get the value of the result, an inverse concept that does not require the direct reference of the operation, called literal
Condition (conditionals): (Look in a series of options and make a decision about what to does based on the circumstances)
Do not translate, probably means that the beauty wears new clothes to go to the street, will first see whether the outside will rain, this is called the condition judgment
if (weather = = "Rain") {Trace ("Take an umbrella bar")}
Loop: If you want to output 5 digits, do you write the trace command 5 times? More of it.
You can use the while and the for command to do repetitive actions.
Events: The code placed inside a frame is executed whenever the movie is played, and the code placed in the MC and button is executed only when the interpreter discovers that the pre-set event is touched. The most typical is the mouse click button caused press time.
function (Functions): A set of programs packaged into a command to call him, in fact, many of the commands in Flash is a function: Trace, play, gotoandstop and so on are.

The fifth chapter starts the first version the choice question production

There are two multiple choice

As you can see, you should develop a good habit of placing code with a separate layer, and putting that layer on top will make it easier to modify.
The second layer places the label separately. Don't get into trouble, as long as you think that most of the world's best customers do so, you won't be bothered.
The following layers are the contents of the selection question, as shown above
Start creating a topic
In the first frame of the question 1 layer, write the topic "When were movie clips introduced into Flash" (When did flash start introducing the concept of movie clips? )
Write three more options: Version 1, version 2, version 3
Then we'll make a selection of the box button, from the library to drag out the three buttons, respectively, placed in front of each option.
The second question is created we use the first question to make a template, select the first frame of the question 1 layer, select the Edit>>copy on the menu Frames
Then select the tenth frame of question 2, Edit>>paste Frames will copy the first question.
The question is, of course, "When is MP3 audio support added to Flash?" (What time does flash start to support MP3 format import? ), the answer is also changed to version 3, version 4, version 5
Data initialization
Although it is a very simple flash, but like other complex flash, first tell Flash to use the data, such as the answer to the question, this is a good habit, the more complex flash, the more benefits
Normally, the definition data should be loading after the end.
In order to set the topic in the first question, action also needs a Stop ();
Select the first frame and enter the code in the Action panel
Init main Timeline Variables
var q1answer; User ' s answer for question 1 The answer to the first question
var q2answer; User ' s answer for question 2 answer to the second question
var totalcorrect = 0; Counts number of correct answers correct questions
var displaytotal; Text field for displaying user ' s score a variable that displays the user's score
Stop the movie at the question
Stop ();
You will find that the first line of code has changed color,//Represents the annotation, is used to assist the reading code, no impact on the execution of the code (yes, you write jokes in it, write novels can, will not affect your flash ^ ^)
We've defined four variables to be used, which are already written in the comments, and it's a good habit to write comments.
About named variable names
Variable name displaytotal, meaning display total, because variable names cannot use spaces and dashes, so capitalize the beginning of the second word to separate the words
Some programmers also like to underline: Display_total
Remember to give your variable a name that can be recognized at a glance.
Add label
We know that the second question is in frame 10th, we only need one sentence as:gotoandstop (10); We can do a second question.
But Moock tells you this is not a good habit, because the content of the animation is constantly changing, so we should be accustomed to using label, even if the number of frames changes, as long as the label points to the correct, do not need to modify the action can continue to run (Chocobo remind you, Do not use the same label, even if the difference is scene.
OK, we create the keyframes in the label layer 1th, 10, 20, respectively, and write them init, Q2, Quizend, F6.
I'm going to write the code for the button.
Write code in three buttons, similar to the following:
Button One:
On (release) {
Q1answer = 1;
gotoAndStop ("Q2");
}
Button Two:
On (release) {
Q1answer = 2;
gotoAndStop ("Q2");
}
Button Three
On (release) {
Q1answer = 3;
gotoAndStop ("Q2");
}
These three codes are described in human language: When a user clicks a button to select an answer, the answer is recorded to the Q1answer variable, and then to the next question.
On this word represents a subsequent event that is to be triggered
Release refers to an event such as releasing the mouse on a button
When the animation executes here, the three buttons will monitor the user's action to release the mouse on the button, and when it happens, the button will automatically execute the code inside the brace {}.
If the user chooses the first, {} is:
Q1answer = 1; Remember that the user chooses the first answer with the Q1answer variable
gotoAndStop ("Q2"); Go to the next question
Above I use the annotation method to explain two code, you must get used to, because not all source program has the tutorial, but the good source program all has the annotation
The second button is similar to the previous one, except that the variable is changed to Q2answer, the end of the screen is selected, for example, the first, to:
On (release) {
Q2answer = 1;
gotoAndStop ("Quizend");
}
The end of the screen
The end of what should be done, of course, is to tell the user the score, written in the 20th frame, is the label for the Quizend frame code:
Tally up the user ' s correct answers
if (Q1answer = = 3) {
Totalcorrect = Totalcorrect + 1;
}
if (Q2answer = = 2) {
totalcorrect++; totalcorrect++ is actually totalcorrect = Totalcorrect + 1; a more concise wording, almost half the letter ah.
}
Show the user ' s score in a on-screen text field
Displaytotal = Totalcorrect;
Employing the language description: If the first question 3, correct number plus one, if the second choice 2, correct number plus one, the correct number of questions given to another variable
Now the correct question number is put in the variable displaytotal, how to show?
At frame 20th of quiz end, type "Thank for taking" the quiz! Your Score Final score is:/2 "(Thank you for answering the question, your final result is correct/2 questions)
In the space before "/2" Put a text box, menu text>>options display text Panel, change the static text down to dynamic text, by the way BORDER/BG Hook removed, Finally, in the variable column, fill in the variable name to be displayed Displaytotal
Are you as impatient as Chocobo? Download Http://www.moock.org/webdesign/lect...oockQuizzes.zip, the QUIZ.FLA is the content of this chapter, and the contents of the other FLA will be mentioned in the following

Sixth chapter to add a bit as knowledge

Data classification is necessary, like 8005154556 such a number is not much sense, but if you classify him to the phone number: 800-515-4556, it makes sense. (Who's on the phone?) Was it moock? Oh
As the type of data is relatively small compared to other languages, there are:
* String strings are composed of a series of characters that can contain letters, numbers, and symbols, typically with double quotes "" (Remember not to write Full-width "")
* Number
* Boolean value Boolean for conditional judgment, only two values true and False
* NULL and Undefined This is also one of the types of data, NULL represents the variable is not content, Undefined is not even a variable is defined
* Array arrays are used to store a set of data sequentially
* MovieClip is this a kind of data? You just like it is a flash unique data bar, is a MC INSTANCE (explain it, MC from the library dragged to the scene is an independent INSTANCE, an MC can create multiple INSTANCE), as well as other data in the MC
* Object can be internally defined by Flash, or a custom type of data when a user writes a program
Sort it again.
Number, String, Boolean, undefined, and null, which belong to a simple data type, characterized by only one value
Array, object, MovieClip. Can contain more than one value
Chocobo: In fact, the array should be regarded as object, above these concepts are always a bit boring, it does not matter, leaving an impression, later used, nature will come back to really understand the

Chapter seventh functions that can be duplicated (function)

Not a few examples to demonstrate it? Why don't you tell me an example? Yes, the next example uses a function, so add a bit of the function knowledge. (Fooled by it?) Chocobo also fooled, began to translate the time did not think this thing so long, this just finished the first example of it 55~
A function is the following:
function FuncName () {
Statements
}
In the action panel, the word function is discolored, and Flash recognizes the word, which represents the content of the function that follows.
FuncName is the name of the function, and when you're going to use it later, you're going to have to hit that name (yes, the build function is to call it back and forth)
() The parentheses inside is the parameter, what is the argument a little later
Inside the brace is a sentence code for the function to complete.
When we create a function, the function does not run, only when you call it out with the name of the function, before it officially starts running.
For example, we have such a function
function Sayhi () {
Trace ("hi!");
}
When we need to sayhi a frame in Flash, enter as:
Sayhi ();
Is it the most common stop () and play ()? Because they're all functions.
The Sayhi function is really intelligent, a meaningful function. First in the scene put a name called Ball instance (please remember to give instance input name, otherwise function run no results don't find me)
Enter such a function in the first frame:
function Moveball () {
Ball._x + 10; Afraid that someone does not understand, explain, _x represents Ball's horizontal axis, ball._x +=10 means ball._x = ball._x + 10, so that the ellipsis of the writing and------*=/= and so on
Ball._y + 10;
}
Make a button again as:
On (release) {
Moveball ();
}
OK, run, from as you can see, with each click of the button, perform a function, and the function lets the ball move down diagonally. (The axis of the flash, the origin in the upper left corner)
Why build a function, just want to be more efficient, now that this function has not been moved every time to write as, but still not enough, we need more extensibility (flexibility) of the function
This function can only move the MC called Ball, only to the fixed direction of moving fixed distance, we need to be able to move any MC, in any direction to move any distance function, so you can save a lot of input as the time (this is called once and for all, hehe)
Our new function has three places that each call is different.
1. The name of the MC that moved
2, the horizontal movement distance
3, vertical movement of the distance (oh, with polar coordinates, you can also say 2, 3 to move the distance, and moving the angle, but we are not accustomed to polar coordinates)
To tell the function where these changes are, we need the parameters (parameters), the parameters are entered when defining the function, and our functions are rewritten:
function Moveclip (theclip, Xdist, ydist) {
Theclip._x + = Xdist;
Theclip._y + = Ydist;
}
When we want to implement the original function function, the call to the new function now becomes
Moveclip (Ball, 10, 10);
function Moveclip when defining functions (Theclip, Xdist, ydist) {
The Theclip parameter (parameters) here is just a definition, essentially nonexistent, because there is no content.
When we use Moveclip (Ball, 10, 10), when the call, the ball input into the theclip, where the ball called arguments (I read less books, will not translate)
Arguments can be either a variable or an expression (such as "a" + "B", which results in a second pass to the function) as long as the arguments are separated by commas.
How does the function end?
Normally, the function runs out of all the statements in {}, we can also use a sentence as:return; Let it end halfway, for example:
function say (msg) {
Return
Trace (msg);
}
When this function is called, it ends with nothing.
There are more important uses for return:
function Sqr (x) {//squares a number squared
return x * x;
}
A=SQR (2); A will be given 2 squared 4.
Return is a more important use of data
In the as inside, there is a built-in function math.sqrt (that is, when you are typing will change color), its function and we just do the SQR function is the same, now you know that the built-in function is not mysterious, we can do the same function.

Eighth chapter the production of the second version choice question

Read this what you found, I found out, Moock is not teaching as, he is in a programming class.
What flaws did you find in the first version of the production? Yes, I have entered many times as, it is troublesome.
We have to do is to use functions to focus our code, only the focus is good to modify, the more complex the more the program is so (imagine a lot of MC between the code, really headache, of course, is to concentrate on a good)
This multiple-choice, we're grouped into two functions. Answer and Gradeuser.
Code (you can see the source program directly, in the above address that zip inside the Quiz-version2.fla):
Most of the code is focused on the first frame, don't be scared by a lot of code, the code is so long, just to make readers see more clearly. (In fact, the shorter the code is more frightening, hehe)
Stop the movie at the question
Stop ();
Initialize main Timeline Variables define variable
var displaytotal; Textfield for displaying user ' s score
var numquestions = 2; Number of quiz questions
var q1answer; User ' s answer for Question1
var q2answer; User ' s answer for Question2
var totalcorrect = 0; Number of questions answered correctly above and example one
var correctAnswer1 = 3; The correct answer to the first question of the correct choice for question 1
var correctAnswer2 = 2; The correct answer to the second question of the correct choice for question 2
function to register user ' s answers functions of this function is to submit the answer to the answering person
function answer (choice) {
answer.currentanswer++;
Now Answer.currentanswer is 1, is submitting the first question, the next answer.currentanswer will become 2, the representative submitted is the second question
Set ("Q" + answer.currentanswer + "answer", choice);
Not complex, not complex, "Q" + answer.currentanswer + "Answer" when the first question is q1answer, the second question is q2answer, the parameter choice passed over the value of two variables inside the
if (Answer.currentanswer = = numquestions) {
Judge whether two questions are answered, is to go to the end of the problem frame
gotoAndStop ("Quizend");
} else {
gotoAndStop ("q" + (Answer.currentanswer + 1));
}
}
function to tally user ' s score is a modifier
function Gradeuser () {
Count How many questions user answered correctly compares two answers to the correct answer, Totalcorrect plus one
Here a For loop, if you have questions, you can look up as a dictionary, in the Empire there are Chinese version
for (i = 1; I <= numquestions; i++) {
The following eval is necessary to say that its role is to make the string and the variable name of a new variable, is a very convenient function
if (eval ("q" + i + "answer") = = eval ("Correctanswer" + i)) {
totalcorrect++;
}
}
Show user's score in a on-screen text field to display the answer, with the first example
Displaytotal = Totalcorrect;
}

Well, the first frame of the function is written, and then the selection button for each answer is simple.
For example, the first question of option one, write:
On (release) {
Answer (1);
}
The second question is written in the same way (if you have a lot of options, the same way, just copy the first question, and then change the title on the line)
Finally, the function Gradeuser () in the Quizend frame is called.
The second example of the analysis is the code, and you'll find a lot more streamlined than the first example.
The code that concentrates on the same frame will:
* More easily modified
* More easily upgraded
* Less chance to make mistakes
* It's easier to check the wrong
* More streamlined (fewer bytes)

Nineth Chapter Array (arrays)

In the next new version of the multiple-choice, what do we use as the features to make it better?
That's the array.
An array is a series of data (Moock begins class again, Chocobo English and computer are not good, in order to avoid fraught, the concept of things as concise as possible)
For example, the data stored in such two variables:
Fruit1 = "oranges";
Fruit2 = "apples";
They are independent of each other, very inconvenient to use, we need an array, the following is the definition of the array method, with "&#;" box, separating each element with ",":
Fruitlist = ["Oranges", "apples"];
Now that the two data is in the same array, we start to explain the arrays in detail.
Each of the data in an array is called an element.
And each element has an independent number to represent the position, the number is called index, note! The first data index is 0, and the second one is 1.
To present data by index, we use an operator &#, such as assigning a value to a with the first element of fruitlist:
A=fruitlist

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.