Programming
Flash MX also enhances its drawing capabilities, and it is now convenient to use the painting methods in the As movie object to let Flash draw graphics from the program. In this paper, I will give you a few examples to explain how to use painting methods to draw graphics and related knowledge and skills.
Effect preview:
Click here to download
To give the reader a good understanding of the following, let's start with a simple example-draw a straight line. Maybe some friends will say, draw a straight line who will not AH. No, let Flash draw itself a straight line.
Open Flash, click on the 1th frame, press F9 to open the Action panel, enter the following code:
Set Line Style
_root.linestyle (3,0xf12345,100);
Set the line start point
_root.moveto (50,0);
Draw lines
_root.lineto (100,0);
To execute the code, you will see that flash automatically starts at the point (50,0), points (100,0) ends, and draws a red horizontal line of 3 pixels wide.
Now, let's drill down and have flash draw a circle. The design of this instance does not need to do any components, only need to write a few pieces of simple code. Open Flash MX, point to Frames 2nd and 3rd, and press F7 to insert a blank keyframe, as shown in Figure 1.
Figure 1
Select the 1th, 2, 3 frames separately, press F9 to open the Actions panel, and type the following code. In fact, the code in these three frames can be in a frame, but in order to give you a clearer idea of how the code works, the author splits the code apart and explains it.
1th Frame Code:
Specify a line style
I=-math.pi;
_root.linestyle (2,0x000000,50);
_root.moveto (50,100);
Code resolution:
The usage of the painting method LineStyle is Mymovieclip.linestyle ([Thickness[,rgb[,alpha]]]). The effect is to specify a line style so that subsequent calls to the LineTo and Curveto methods are called until LineStyle is invoked with other arguments. You can also call the LineStyle method in the middle of the drawing path to specify a different style for the different line segments in the path.
where thickness is an integer that indicates the thickness of the line in points, with a valid value of 0 to 255. If no numeric value is specified, or if the parameter is undefined, the line is not drawn.
RGB is the hexadecimal color value of the line (for example, Red is 0xff0000, Blue is 0x0000ff). If this value is not indicated, 0x000000 (black) is used by default.
Alpha is also an integer that controls the alpha value of the line color, with a valid value of 0 through 100. If this value is set, 100 (solid color) is used. If the value is less than 0, use 0, or 100 if the value is greater than 100.
The use of painting methods MoveTo is Mymovieclip.moveto (x,y). The effect is to move the current painting position to (X,y), which is to set the origin of the drawing image. If any one of the parameters is missing, this method fails and the current painting position does not change. The parameter x (y) is an integer that indicates the horizontal (vertical) position of the registration point relative to the parent movie clip. The _root.moveto (50,100) is the beginning of the drawing, starting at a point where the x coordinates are 50 units and the Y coordinate is 100 units.
2nd Frame Code:
Draw lines
_root.lineto (100+50*math.cos (i), 100+50*math.sin (i));
i+=0.05;
Code Analysis:
Painting method LineTo You can use line styles to draw lines from the current painting position to point (x,y), and the current painting position is subsequently set to (X,y). If the movie clip you are drawing contains content created with the drawing tool, then calling LineTo will draw below that content. If the LineTo method is called before any calls are made to the MoveTo method, the current painting position defaults to (0,0). If any one of the parameters is missing, this method fails and the current painting position does not change. The parameter x (y) is an integer that indicates the horizontal (vertical) position of the registration point relative to the parent movie clip.
Math.sin (x) calculates and returns the sine value of the angle specified in radians. The argument x is the angle value in radians.
3rd Frame Code:
Cycle
if (I<math.pi) {
gotoAndPlay (2);
} else {
Stop ();
}
Math.PI is a constant that represents the mathematical constant of the ratio of the circumference of a circle to its diameter, which is 3.1415 ....
The code is written here. When the test, you can see, flash from the point (50,100), draw a line width of 2 units, black, radius of 50 units of the circle. As shown in Figure 2.
If you want to do all of the things in 1 frames, write the following code:
_root.linestyle (2, 0x000000, 50);
_root.moveto (150, 100);
Function C () {
_root.lineto (100+50*math.cos (i), 100+50*math.sin (i));
i + 0.05;
}
if (I<math.pi) {
Perform function C every 50 milliseconds
SetInterval (c, 50);
} else {
Stop ();
}
To teach you this code, there are two main intentions. First, learn how to use SetInterval (). This action calls a function, method, or object at every interval of time when the movie is played. You can also use this action to update a variable or update time display from a database. The second, the author of the circle to draw the beginning of a change, as shown in Figure 3. This way, the position of the drawing line changes. When drawing graphics, the position control and prediction of the graphic is important, otherwise it will be a mess.
We have learned how to draw a graph, then, how to color the drawing of the plot? Next, we're going to color a rectangle. Open Flash, click on the 1th frame, press F9 to open the Action panel, enter the following code:
Set Fill Color
Beginfill (0xff0000,100);
LineStyle (3,0xff9900,100);
MoveTo (100,50);
LineTo (200,50);
LineTo (200,150);
LineTo (100,150);
LineTo (100,50);
End Coloring
Endfill ();
Code Analysis:
Painting method Beginfill: Indicates the beginning of a new painting path. Mymovieclip.beginfill ([Rgb[,alpha]]). The parameter GB is a hexadecimal color value (for example, 0xff0000). If the value is not provided or not defined, the fill is not created. The parameter alpha is an integer between 0 and 100 that specifies the alpha value of the fill. If the value is not provided or the value is greater than 100, 100 (solid color) is used. If the value is less than 0, use 0.
A painting method similar to Beginfill Begingradientfill determines the fill form, color, transparency, proportions, and matrix values. Usage: Mymovieclip.begingradientfill (filltype, colors, alphas, ratios, matrix). Where Filltype is a string linear or string radial. Colors is an array that includes RGB hexadecimal color values to be used in gradients. Alphas is also an array that includes alpha values corresponding to the colors in the colors array. An array of ratios color quotas; Valid values are 0 to 255. The value, by 100%, defines the percentage of the width at which the color is sampled. The matrix is a variant.
Well, here's the end of the article. I hope I can help my friend who likes to use flash painting.