Entry-Level tips: FLASH programming experience

Source: Internet
Author: User
Tags modify variables range reference advantage
Programming | skills | experience

In fact, my level is not very good, today the reason for a programming experience (mainly their own little experience summary) introduction, mainly to help interested in the deep study as friends, because of the limited level of individuals, which inevitably have fallacies, we welcome discussion to correct. To help me advance, and more importantly, to prevent beginners from developing bad programming habits! A lot of crap, here's the start:

A. Flash has three places to add code, keyframes, MC (movie clips), buttons.

1. The flexibility of the code on the key frame is very high, it is suggested that beginners: can write the code on the MC or button, try not to write on the key frame, after the key frame adds the code, will appear a A, like figure A:


2The general format for adding code on buttons is:
On (MouseEvent) {
statements;
MouseEvent: Triggers an event keyword that indicates that an event is to be jumped.
Statements: Can execute program code for any number of rows. Mouse can be pounced on the following events:
Press: Triggers the event when the button is pressed
Release: This event is triggered when the button is released
Releaseoutside: Triggers the event when the button is pressed and the mouse is moved outside the button and released
RollOut: Triggers the event when the mouse slides out of the button range
Rollover: Triggers the event when the mouse slides into the button range
Dragout: Triggers the event when the button is pressed by the mouse and dragged out of the button range
DragOver: This event is triggered when the button is pressed by the mouse and dragged into the range of the button
KeyPress ("key"): Triggers the event when the keyboard key specified by the parameter (key) is pressed, for example:
On (Press) {
_root.text= "Learning flash! "
}//the text box in the main scene displays text when the button is pressed: Learn flash!


3. General format for adding code on MC:
Onclipevent (movieevent) {
statements;
}
Movieevent: Triggers an event keyword that indicates that an event is to be jumped.
Statements: Can execute program code for any number of rows, representing the program code block that is subordinate to the event.
The following events can be seized:
Load: This event is triggered before the current MovieClip is mounted and ready to be displayed
Unload: This event is triggered before the current MovieClip is uninstalled and ready to disappear
Enterframe: The current MovieClip triggers the event every time the content on the frame is computed
MouseMove: Triggers the event when the mouse is moved
MouseDown: Triggers the event when the left mouse button is pressed
MOUSEUP: Triggers the event when the left mouse button is raised
KeyDown: This event is triggered when the keyboard key is pressed
KeyUp: This event is triggered when the keyboard key is raised
Data: This event is triggered when new data is received by the current MovieClip. For this issue, refer to my tutorial (which has a detailed example):

Onclipevent () is a movie clip event management action. According to the programming specification of Flash, the movie clip action must be triggered by the movie clip event, and the specific trigger condition can be added in its parameter area. The options in its parameter bar include: Load Unload enterframe Mousemove Mousedown Mouseup Keydown Data (total 9 items). The following are described in turn:

1.Load------Trigger event when a movie clip symbol appears in the timeline (triggered regardless of whether the movie clip is fully loaded) and only once!

2.Unload-------Trigger event when the movie clip symbol is unloaded in the timeline;

3.EnterFrame--------perform one trigger per frame when playing the contents of a movie clip symbol;

4.mouseMove--------triggers whenever the mouse is moved. The change in the mouse position triggers the event. You can use the _xmouse _ymouse property to determine the current mouse coordinates;

5.mouseDown--------triggered when the left mouse button is clicked;

6.mouseUp--------Trigger When releasing the left mouse button;

7.keyDown--------Trigger when pressing a key on the keyboard;

8.keyUp--------Trigger when releasing a key on the keyboard;

9.Data------triggered when a loadvariables or Loadmovie action is received to load new data;

For specific applications, see the following SWF:


Here's another simple example:

Onclipevent (load) {
a=100;
b=0;
}//a b two variable to an initial value when the MC is loaded;


  Two. Syntax Related:

1. The first and foremost is the point grammar, which is programmed to indicate the properties and methods associated with an object, and to identify the path to the object.

For example, to get Myball in the main scene, the x-coordinate of this movie clip is _root. Myball._x. One of them has to be noticed, and beginners often assume that the myball here refers to naming objects in the library, as shown in Figure two:


In fact, this is wrong, the name appearing in the program, we are accustomed to known as the instance name, its correct naming method is to select the object in the scene to be named, and then name it in its property panel, as shown in Figure three:


Of course, in the loading of external images, SWF or directly call the library voice, MC, it can also through the program itself directly to the object to add an instance name, these skills will be later encountered when to give an explanation! The point syntax has two special names in the object name: _root and _parent, where _root represents the main scene, and _parent can read and set any attributes that contain the parent object of the current object. I do not know how to develop the habit, very little use of _parent, I prefer to use all from the main scene to locate the object method. Sometimes such positioning is not as convenient as the combination of two positioning methods, but I think the most outstanding advantage is to make mistakes, because your path is always from top to bottom (from the main scene to an internal MC or button) which level out of the problem is easy to see, do not have to switch back and forth between the MC and the scene, Look Who's the father, who's the son! In addition, I have a habit, even if the object is in the home scene, I also like to control his time to add a _root. For example, in the main scene of the Myball this MC is a moving ball, to use the button control to stop him, the following statement can be implemented:

On (release) {
Myball.stop ();
}

But I usually write:

On (release) {
_root. Myball.stop ();
}


Of course, it's just my personal habit, and maybe everyone sees that I'm asking for trouble. However, I speak out because I think it is a habit, is to maintain a path of the concept in mind. This should be quite important.

2. About slash syntax:

As far as I know, the slash syntax is widely used in Flash 4.0 and is also used to indicate the path of an object and is used instead of a point, when he wants to point out that a variable needs to be preceded by a colon to represent a reference to a variable. Since software has a backward compatibility, it can still be used in MX (I haven't used 2004 estimated 2004 also supported), but generally said not recommended use, on the one hand, since replaced, it must be because there are shortcomings, there is a good way to embrace the old do not put (if you are afraid of being said that the new, Then you keep using it! I am not afraid of, digging haha ... On the other hand, in order to keep in line with other people, we all use a little grammar, you are a person with a slash, always feel very good!

3. Semicolon: Programming, I have at the end of each sentence plus the habit of semicolons, in fact, this semicolon can be omitted, then why should I add it, hey heh ... That is because I started learning to use flash programming, no one told me can not add, I have been working hard to add AH!!! I can't change it now! Digging haha ........

4. The case of Flash programming, only keywords to distinguish between uppercase and lowercase, what is the keyword, basically you write the right, will become a blue part of the program code, that is, for the language-specific words!! For example: SetProperty clearinterval getRGB these! 5. Annotate; Add comments generally to facilitate others to read your program, after adding the annotation symbol//, then the content is gray; in fact, he has one of the biggest advantage is that it is convenient for you to debug the program, such as compile-time discovery of the program is wrong, but the program is very long, you can not exactly determine the location of the error occurred, Then you can use//segment to block off the program section, and then help determine the wrong location.

  Three. About Error checking:

Often from the idea to the implementation of the program can not be completed once, that is, it is inevitable that there will be errors, can not compile the system (do not know how to call the Flash, said to compile the system is not right, but also please advise). That's what we need to find out right now. 1. Make full use of the prompts from the Output window: For example: Add the following code to the button:

Duplicatemovieclip ("MC", "MC" +j,10+j);
C=random (40);
SetProperty ("MC" +j,_y, (c+30));
SetProperty "MC" +j,_x,random (600));
SetProperty ("MC" +j,_alpha, (2*c+20));
j + +;
if (j>700) {
J=1;
}

Ctrl+enter test movie, Output window hint:

Scene = Scene 1, layer =as, frame = 2: Line 4th: Unknown error ID #1033 setProperty "MC" +j,_x,random (600)); That is, the fourth line has errors, back to the editing state, a look, the original less than half the parentheses, that is, the Output window will provide us with a lot of necessary help, we must make good use of! Let the as edit area display the line number method, as shown in Figure four:


Click on the button in the red circle, select the display line number can be! Other buttons can also achieve a lot of auxiliary functions, to take a look at their own to know!

2. The above method can only exclude grammatical errors, that is, your program itself or the process of the transfer of parameters, there is a problem, the compiler system is impossible to help you check out, this can only rely on your own to check. I'm here to introduce some of my own methods, for everyone's reference: First of all, you have to understand their own minds in accordance with this algorithm can realize the desired results, that is, the first in the heart of the whole algorithm again, sometimes think of immature, or a beginning of the idea of omission, this time should be able to correct or modify it! When you are sure that your program design is not a problem but still can't work, you should consider that there are errors in the implementation of the program, such as the problem of parameter passing, the path indication is not clear or even wrong, etc. are more common (at least I often make such a mistake); For example: there is an input text in the home scene, In fact, the example name is the text variable named XX,
Then the frame of the home scene adds:

_root.text.onchanged = function () {
N=number (XX);
}

Assign the value of the text box to n when the contents of the text box change; Then in the home scene a windmill-shaped MC is added:

Onclipevent (enterframe) {
This._rotation=this._rotation+n;
}

It means let the windmill rotate, the speed is N, but the test film is no effect, why, think about it, first the code on the windmill to read:

Onclipevent (enterframe) {
this._rotation=this._rotation+3;
}

Specify a number as the speed of rotation, the test found that the rotation is normal; this reminds us, is not the parameter passed out the problem, Oh!! Yes, because the code added to the frame, the resulting variable n is actually a variable in the main scene, when passing him into the MC, the exact position should be determined. To modify the source code again, the code on the MC should read:

Onclipevent (enterframe) {
THIS._ROTATION=THIS._ROTATION+_ROOT.N;
}

Test again, everything is normal, rotate speed changes with the input number change effect:

http://www.flash8.net/bbs/UploadFile/2004131491788994.swf

Click here to download the source file

3. Learn to use trace statements to detect errors! In the example above, there are fewer variables, so it's easy to see the problem, if there are multiple variables in a program, and more complex, then you need to track them separately, by using the trace statement, after each change in a variable with trace to the output, To see if the results are the same as expected! Also for example: Random lottery Effect! 10 MC, transparency is 30; key frame in main scene plus:

A =new array (0,1,2,3,4,5,6,7,8,9);//define arrays;
mm=0;//click Count;
The number of nn=10;//array elements; Select button Plus: On (press) {
i = random (NN);
j = A[i];
A1=a.slice (0,i);
Trace (A1);//output the number of parts extracted by the new array A1;
A2=a.slice (I+1,NN);
A new array A2 of Trace (A2);//The other fraction of the output extraction;
A=a1.concat (A2);
Trace (a);//output combination of the array, that is, the new array A;
SetProperty ("MC" +j,_alpha,getproperty ("MC" +j,_alpha) +30);
mm++;
nn--;
if (mm==10) {
Text= "Draw to the end!" "
Mm= "10";
_root.but._visible=false;

}
}

Using the above method, each click of a button, the window output three rows of digits, the last row of the previous output is less than the number of the number is extracted; Thus, every time the code is run, the program staff can easily view the operation of each variable! Effect: (Note that the trace statement can actually be used specifically for error checking because he is not valid in the SWF file, so the trace window cannot be displayed in the following effects, and you can download the following source file for the native test):

http://www.flash8.net/bbs/UploadFile/20041314105332216.swf

Click here to download the source file



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.