Document class)
Now we have some knowledge about the class. Next, let's take a look at how to use it. Sometimes I often say how important SWF is based on as 3, because as 3 introduces a new concept, document class ).
A document class is a class that inherits from sprite or movieclip and serves as the main class of SWF. When SWF is read, the constructor of this document class is automatically called. It becomes usProgramYou can write anything you want to do, such as creating video clips, drawing pictures, and reading resources. If you writeCodeYou can use the document class or choose to continue writing code on the timeline. However, if you use flex builder 2 or the free flex SDK, there is no timeline, and the only way is to write it in the class. All of this work revolves around a powerful document class, without which there is no SWF. The following is a framework of document classes:
Package {
Import flash. display. Sprite;
Public class test extends sprite {
Public Function Test (){
Init ();
}
Private Function Init (): Void {
// Write code
}
}
}
If you have read the previous section, you will not consider it a new knowledge, but put them together. Use the default package to import and inherit the sprite class. There is only one constructor that calls the init method. Of course, you can also write all the code in the constructor, but you need to develop a good habit of minimizing the code in the constructor, so you can write the code to another method. This book will give you a lot of code blocks for testing. At that time, you should put the code blocks into the init method as in the above example. In this way, the code in init will be called during the compilation and execution of the film. Next we will begin to learn how to connect the document class and SWF.
Use Flash CS3 IDE (integrated development environment)
Flash CS3 IDE is the most convenient tool for implementing document classes. Select a folder for the above classes to save. The file name is test.. Open Flash CS3, create a FLA file, and save it to a directory of the same class. Make sure that FLA is set to Flash Player 9 and as 3.0 by default. On the Properties panel, we noticed that a region named document class (Figure 2-1) appears ). You only need to enter the Class Name: test.
Figure 2-1 set document class
Note that we enter the class name instead of the file name. Therefore, you do not need to enter the extension.. If this class is included in a package, you need to enter the complete path of the Class-for example, Com. friendsofed. chapter2.test.
Program Animation
Next, let's learn some basic principles of as 3 programming. If you have already selected a development environment, proceed. Let's enter the ActionScript animation world.
Animation Execution Process
Almost all program animations include several different execution processes. Frame-by-frame animation means creating and storing a set of continuous bitmaps. Each frame is an image and only needs to be displayed, as shown in Figure 2-3.
Figure 2-3 frame-by-frame animation
When we use graphics or components in flash, things have undergone subtle changes. In this case, flash will not create and store new bitmaps for each frame. For each frame, flash stores the position, size, color, and so on of each object on the stage. For example, if a ball moves on the screen, each frame only stores the position of the ball on this frame, and the position of the 1st-frame ball is 10th pixels on the left, 2nd frames may be 15th pixels, and so on. The Flash Player reads the information and then renders the stage and displays the frame based on the description. Expand the flowchart based on these changes, as shown in Figure 2-4.
Figure 2-4 render and display frames
This is how I describe a dynamic program animation, as shown in Figure 2-5.
Figure 2-5 script Animation
As shown in 2-5, there is no 1st frame or 2nd frame concept. Script animation is usually completed only by one frame. Next, let's take a look at the animation execution process.
First, create initialization. Place a video clip in the stage, create a complementary animation, or use code to describe the entire scenario. In short, the frame must be rendered and displayed.
Then, apply the custom rules. The rule can be as simple as "Move the ball to the right five pixels", or it can be composed of dozens of complex trigonometric functions. When you use a custom rule, a new description is generated and then rendered and displayed based on the description, and the rule is continuously applied.
Please note that the same rule is executed over and over again, instead of using a set of rules for 1st frames, and another set of rules for 2nd frames. Therefore, the difficulty lies in that a set of rules should deal with all possible situations. What if the ball moves too far to the right beyond the stage? Your rule will solve this problem. Do you still want the user to operate the ball with the mouse? Then your rules should also be taken into account.
It sounds complicated. In fact, the "rule" mentioned here is actually the ActionScript code. Each rule can consist of one or more lines of code. The following example shows how to move a ball 5 pixels to the right:
Ball. x = ball. x + 5;
This statement means that no matter where the X coordinate of the ball is (horizontal axis), it adds 5 pixels to the original X coordinate and uses the coordinate as its new x coordinate. It can also be simplified:
Ball. x + = 5;
"+ =" OPERATOR: adds the value on the right to the variable on the left, and then assigns the value to the variable. The following are more advanced rules that will be learned in the future:
Code
VaR DX: Number = Mousex - Ball. X;
VaR DY: Number = Mousey - Ball. Y;
VaR Ax: Number = DX * Spring;
VaR Ay: Number = Dy * Spring;
VX + = Ax;
Vy + = Ay;
Vy + = Gravity;
VX * = Friction;
Vy * = Friction;
Ball. x + = VX;
Ball. Y + = Vy;
Graphics. Clear ();
Graphics. linestyle ( 1 );
Graphics. moveTo (ball. X, ball. y );
Graphics. lineto (mousex, Mousey );
It doesn't matter if you know that flash will generate this code in each frame and execute it continuously.
How to make it run cyclically? Let's take a look at my first attempt. This is also a mistake that many as beginners will make. This is a loop structure that exists in many programming languages, such as for and while. Use a loop structure to execute code repeatedly. This is what I wrote:
For (I = 0 ; I < 500 ; I ++ ){
Ball. x = I;
}
It looks quite simple. Variable I starts from 0, so the X coordinate of the ball moves to 0 -- the leftmost of the stage. I ++ increases the I value by 1 at a time, that is, 0 ~ 1 ~ 2 ~ 3 ~ 4 ..., Each time this value is used as the value of ball. X, the ball is moved from left to right. When the value is 500, the expression I <500 value is false, and the loop ends.
If you make the same mistake, you will know that the ball did not move on the stage-it just suddenly appeared on the right of the stage. Why didn't I move to those points in the middle? It actually moved, but we didn't see it, because we didn't let flash refresh the screen. Figure 2-6 shows another flowchart to see what actually happened.
Figure 2-6 why the loop structure cannot produce an animation
In fact, we used custom rules to move the ball to the specified position and created 500 new scenes. However, no display is provided before the loop ends, because Flash only refreshes once after each frame ends, which is very important. The following is the sequence in which flash enters the frame:
- Place all objects on the stage, no matter what level, layer, or whether the film is loaded.
- Execute all action scripts on the frame, regardless of the level, layer, whether in the video clip or button, or where it is nested.
- Determines whether the display is displayed. If the frame rate is set to 20 frames per second, flash will display the frame at least 50 milliseconds after the last frame is displayed. After the frame is displayed, it is necessary to compile and enter the next frame. If the frame rate does not reach 20 frames per second, wait until the correct time before execution.
There are some problems with the scheduled time. First, we all know that the frame rate is not accurate (even in Flash 9), and do not rely on it as a precise timer. Second, it takes more time to compile and execute.
Even so, we don't have to worry that our scripts will be cut down. Before entering step 1, flash will complete all executable code (step 2), even if you need to delay the frame rate. In order to complete the script, flash may even wait for 15 seconds. In the above example, flash waits until the loop ends and then enters the next frame. The screen is refreshed only when the next frame is redirected. That's why we see the beat instead of moving. Therefore, to complete the movement, all we need to do is scatter this loop. Please refer to Figure 2-5.