Using as to achieve the animation of the snow effect

Source: Internet
Author: User
Tags eval
Nothing, just the following copy of the as code into the Flash MX new document frame 1th, CTRL + ENTER Test, you will see the snowflake effect.

The code is as follows:
//=========

S_x = Stage.width;
s_y = Stage.height;
S_snow = 120;
Tturl= "Www.webjx.com"
_root.createemptymovieclip ("Webjx_bg", 1);
With (_ROOT.WEBJX_BG) {
Beginfill (0x000000, 100);
LineStyle (0, 0x000000, 0);
MoveTo (0, 0);
LineTo (s_x, 0);
LineTo (s_x, s_y);
LineTo (0, s_y);
LineTo (0, 0);
Endfill ();
}
_root.createemptymovieclip ("Webjx_snow", 2);
With (_root.webjx_snow) {
LineStyle (0, 0x000000, 0);
Beginfill (0XFFFFFF);
MoveTo (20, 20);
Curveto (24, 20, 24, 16);
Curveto (24, 12, 20, 12);
Curveto (16, 12, 16, 16);
Curveto (16, 20, 20, 20);
Endfill ();
}
_root.webjx_snow._visible=0
WEBJX = 3;
while (webjx<=s_snow&&tturl== "www.webjx.com" &&substring (_root). Tturl, 5, 5) = = "Webjx") {
Duplicatemovieclip ("Webjx_snow", "Snow" +webjx, WEBJX);
With (eval ("Snow" +webjx)) {
_x = random (s_x);
_y = random (s_y);
_xscale = Math.random () *60+40;
_yscale = eval ("Snow" +webjx). _xscale;
_alpha = eval ("Snow" +webjx). _xscale+random (15);
}
webjx++;
}
for (var i = 3; i<=s_snow; i++) {
var truewebjx = ["Snow" +i];
_root[truewebjx].onenterframe = function () {
This._x + = Math.random () * (This._xscale)/10;
This._y + = Math.random () * (This._yscale)/10;
if (this._x>_root. S_x) {
this._x = 0;
}
if (this._y>_root. s_y) {
this._y = 0;
}
};
}


//=========================
End of code

How to see the effect of it:
Now I'm going to say what I'm thinking about when I'm making a program:

We want to do the snow effect, that must have white snowflakes, so with Createemptymovieclip () to create a small white round MC is imperative ...
However, do not forget that in Flash MX, the default color of the background is white, so if we do not see the effect of snowflakes falling (because snowflakes and backgrounds are white), the first step is to create a black rectangle with createemptymovieclip (), which is the same size as the main scene.
S_x = stage.width;//The width of the main scene
S_y = stage.height;//The height of the main scene
Used to get the width and height of the main scene.

Here is a black background to make
_root.createemptymovieclip ("Webjx_bg", 1);
With (_ROOT.WEBJX_BG) {
Beginfill (0x000000, 100);
LineStyle (0, 0x000000, 0);
MoveTo (0, 0);
LineTo (s_x, 0);
LineTo (s_x, s_y);
LineTo (0, s_y);
LineTo (0, 0);
Endfill ();
}
Use Createemptymovieclip ("WEBJX_BG", 1) to create a new MC (movie clip). The variable s_x and s_y are used to denote the width and height of the main scene. After drawing the rectangular wireframe, fill the large MC with the main scene with Black.

The first step has been completed. Can CTRL + ENTER test to see if there is a black rectangle in the home scene?

All right, let's do the Snowflake again:

Make a snowflake and fill it with a small white circle.
_root.createemptymovieclip ("Webjx_snow", 2);
With (_root.webjx_snow) {
LineStyle (0, 0x000000, 0);
Beginfill (0XFFFFFF);
MoveTo (20, 20);
Curveto (24, 20, 24, 16);
Curveto (24, 12, 20, 12);
Curveto (16, 12, 16, 16);
Curveto (16, 20, 20, 20);
Endfill ();
}
CTRL + ENTER Test to see if there is a small white spot in the upper left corner of the scene?

Okay, let's go down:
Since it is snowflakes flying, it is impossible to have only one snowflake in the fly so we have to use Duplicatemovieclip () to copy more than n snowflakes, in order to modify the future or when others look convenient, we put the number of snowflakes with variables to replace:
S_snow = number of 120;//Snowflakes
_root.webjx_snow._visible=0//the original snowflakes hidden
WEBJX = 3;
while (webjx<=s_snow&&tturl== "www.webjx.cn" &&substring (_root). Tturl, 5, 5) = = "Webjx") {
Duplicatemovieclip ("Webjx_snow", "Snow" +webjx, WEBJX);
With (eval ("Snow" +webjx)) {
_x = random (s_x);
_y = random (s_y);
_xscale = Math.random () *60+40;
_yscale = eval ("Snow" +webjx). _xscale;
_alpha = eval ("Snow" +webjx). _xscale+random (15);
}
webjx++;
}

OK, now test again to see if the sky snowflakes:

Although it is a sky of snowflakes, but do not move how to do?
It doesn't matter, we can still use as to achieve d~~~
Look:

Control the flutter of snowflakes, and if they drift out of the main scene, return to the main scene
for (var i = 3; i<=s_snow; i++) {
var truewebjx = ["Snow" +i];
_root[truewebjx].onenterframe = function () {
This._x + = Math.random () * (This._xscale)/10;
This._y + = Math.random () * (This._yscale)/10;
if (this._x>_root. S_x) {
this._x = 0;
}
if (this._y>_root. s_y) {
this._y = 0;
}
};
}
Using a For loop to control all the copied snowflakes, we can use Math.random () to enhance the random effect.


Well, that's it, let's try it yourself:


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.