Moving Clouds and Waving Grass
Description: Add puffy clouds and wavy grass to your movies
Author: John Bezanis
Added: May 23rd 2007
Version: Flash 8
Total Views: 29806
Views in the Past 7 Days: 262
This movie contains two symbols, clouds and grass. The clouds move at a constant speed and are generated dynamically. Each time a cloud moves past the screen to the left, a new cloud is created with a random shape and color. The grass is plotted at certain coordinates when the movie starts. Each blade of grass is set to a random color. At the start of each frame, the grass waves according to an x position where the blades of grass are attracted to. The code for the clouds and grass can be accessed by opening their movie clip in the library.
Code for the clouds:
- //Number of clouds
- clouds=6;
- //These are just general boundaries.
- //To use exact boundaries, create a mask in the parent level of the size desired
- //Height of the sky
- skyheight=Stage.height;
- //Width of the sky
- skywidth=Stage.width;
- //Max size of a cloud
- cloudsize=300;
- //Amount of blur applied to the shapes to make them cloud-like
- blursize=40;
- //Clouds move at a random speed. this is the minimum speed
- cloudminspeed=.5;
- //Variance in speed from cloud to cloud
- cloudspeedvariance=1;
- //Create the clouds
- for(c=1;c<=clouds;c++){
- //create an empty movie clip to hold the cloud
- this.createEmptyMovieClip("cloud"+c,this.getNextHighestDepth());
- //generate a cloud. Pass in the instance name of the newly created cloud
- shapecloud("cloud"+c);
- //Set the x position to a random position within the boundaries
- eval("cloud"+c)._x=Math.random()*skywidth-eval("cloud"+c)._x/2;
- //Set the y position to a random position within the boundaries
- eval("cloud"+c)._y=Math.random()*(skyheight)-eval("cloud"+c)._height;
- }
- //Run at the start of each frame
- onEnterFrame=function(){
- //Run for each cloud
- for(c=1;c<=clouds;c++){
- //Move the cloud to the left according to its speed
- eval("cloud"+c)._x-=eval("cloud"+c).cloudspeed;
- //If the cloud is past the stage to the left, reset it to the right. Create a new shape and color
- if(eval("cloud"+c)._x+(eval("cloud"+c)._width/2)+cloudsize<0){
- //Reset the x position
- eval("cloud"+c)._x=skywidth;
- //Reshape and recolor the cloud
- shapecloud("cloud"+c);
- }
- }
- }
- //This function creates the shape and color of a cloud
- function shapecloud(cloudid){
- //Clear the current contents of the cloud
- eval(cloudid).clear();
- //Set the new shade between 224 and 255. This number is used for the red, green, and blue, to create a grayscale color
- cloudcolor=Math.round(Math.random()*31)+224;
- //Use no line
- eval(cloudid).lineStyle(undefined, (cloudcolor+cloudcolor*0x100+cloudcolor*0x10000), 100, false, "none", "none", "none", 1);
- //Set the fill color. cloudcolor is used 3 times, for red, green, and blue
- eval(cloudid).beginFill((cloudcolor+cloudcolor*0x100+cloudcolor*0x10000));
- //Set a starting coordinate for the cloud
- eval(cloudid).moveTo(Math.random()*cloudsize,Math.random()*cloudsize);
- //Draw an invisible line to another point the combined lines form shapes, which are the clouds.
- //They don't look much like clouds until the blur is applied
- eval(cloudid).lineTo(Math.random()*cloudsize,Math.random()*cloudsize);
- eval(cloudid).lineTo(Math.random()*cloudsize,Math.random()*cloudsize);
- eval(cloudid).lineTo(Math.random()*cloudsize,Math.random()*cloudsize);
- eval(cloudid).lineTo(Math.random()*cloudsize,Math.random()*cloudsize);
- eval(cloudid).lineTo(Math.random()*cloudsize,Math.random()*cloudsize);
- //Apply a blur to the shape
- eval(cloudid).filters = [new flash.filters.BlurFilter(blursize,blursize,2)];
- //Set a new cloud speed
- eval(cloudid).cloudspeed=Math.random()*cloudspeedvariance+cloudminspeed;
- }
- //Height of each blade of grass
- grassheight=35;
- //Average space in between each blade of grass
- grassspacing=5;
- //Maximum sway of each blade of grass
- maxsway=20;
- //Number of blades of grass along the x axis
- xplots=30;
- //Number of blades of grass along the y axis
- yplots=20;
- //The wind has an x position and the grass is attracted to the position
- windxpos=0;
- //Velocity of the wind left and right
- windspeed=0;
- //Gives the grass a bent effect. The grass bends 1/4 of the way up
- grasscontrol=grassheight/4;
- //Array containing the info for each blade of grass
- grasscoords=[];
- //These loops go through the field, planting each blade of grass
- for (xpos=0; xpos<xplots; xpos++) {
- for (ypos=0; ypos<yplots; ypos++) {
- //x position, y position, sway, and color
- grasscoords.push([xpos*grassspacing+Math.random()*grassspacing,ypos*grassspacing+Math.random()*grassspacing,0,Math.round(Math.random()*128)*65536+Math.round(Math.random()*76+146)*256]);
- }
- }
- //Run on each frame
- onEnterFrame=function(){
- //Clear all of the grass so it can be redrawn with different sway
- this.clear();
- //Change the speed of the wind
- windspeed=Math.max(-50,Math.min(50,windspeed+Math.random()*40-20));
- //Move the position the blades are attracted according to the windxpos
- windxpos+=windspeed;
- //If the windxpos moves too far to the left, reverse its speed
- if(windxpos<-100){
- windxpos=-100;
- windspeed*=-1;
- }
- //If the windxpos moves too far to the right, reverse its speed
- else if(windxpos>grassspacing*xplots+100){
- windxpos=grassspacing*xplots+100;
- windspeed*=-1;
- }
- //handle the redraw for each blade of grass
- for(coord=0;coord<grasscoords.length;coord++){
- //Set the line style. 0 means use hairline width and grasscoords[coord][3] is the color
- this.lineStyle(0, grasscoords[coord][3], 100, false, "normal", "none", "none", 1);
- //Adjust the sway according to the grass's current sway and the windxpos
- grasscoords[coord][2]=Math.max(-maxsway,Math.min(maxsway,grasscoords[coord][2]+Math.max(-maxsway,Math.min(maxsway,(windxpos-grasscoords[coord][0])/100))))+(Math.random()*3-1.5);
- //Move to the base of the blade of grass
- this.moveTo(grasscoords[coord][0],grasscoords[coord][1]);
- //Draw a curved line to the new top of the blade of grass
- this.curveTo(grasscoords[coord][0],grasscoords[coord][1]-grasscontrol,grasscoords[coord][0]+grasscoords[coord][2],grasscoords[coord][1]-grassheight+Math.abs(grasscoords[coord][2]/2));
- }
- }
Download the source file below:
Download Source File
Questions and Suggestions
hi guys
June 11th 2007 16:06PM - ebay
Totaly
Bounceeeeeeeeeeeeeeeeeeeerrrrrrrrrrrrrrrrrrrrrrrr............
June 29th 2007 05:06AM - Dharam
I have a great code for rain (or snow) that i modified to make it blow left (in the wind).
1. Make a line like this:
/
(W:2.5)
(H:4.3)
2. Convert it to a movie clip. Call it rain and give it a linkage identifier of rain.
3. Delete the movie clip on the stage and add this script to the first keyframe:
init = function () {
width = 1000;
// pixels
height = 400;
// pixels
max_snowsize = 10;
// pixels
snowflakes = 1000
// quantity
for (i=0; i<snowflakes; i++) {
t = attachMovie("rain", "rain"+i, i);
t._alpha = 20+Math.random()*60;
t._x = -(width/2)+Math.random()*(1.5*width);
t._y = -(height/2)+Math.random()*(1.5*height);
t._xscale = t._yscale=50+Math.random()*(max_snowsize*10);
t.k = 1+Math.random()*2;
t.wind = -1.5-1;
t.onEnterFrame = mover;
}
};
mover = function() {
this._y += this.k;
this._x += this.wind;
if (this._y>height+10) {
this._y = -20;
}
if (this._x>width+20) {
this._x = -(width/2)+Math.random()*(1.5*width);
this._y = -20;
} else if (this._x<-20) {
this._x = -(width/2)+Math.random()*(1.5*width);
this._y = -20;
}
}
init();
That's it!
PS. This works best with a high frame rate.
July 22nd 2007 17:07PM - ME!
//Number of blades of grass along the y axis
yplots=20;
skyheight=Stage.height;
August 18th 2007 13:08PM - claud
Awesome thnx but could you make so it acts BELOW other layers cuz Im Using it for a project but the clouds go Over my other layers
November 8th 2007 12:11PM - leo
Leo, you could create a movie clip at the layer (level) you want, and then place the clouds into that layer.
November 11th 2007 19:11PM - John
How to create Free loader In Flash ?
(Tell Clearly Pls)
November 18th 2007 11:11AM - SURESH.V
I want to use hit test to make my character not move when it rich the walls
December 5th 2007 08:12AM - Ephenia
Awesome code guys! I was wondering if it was possibly to generate random clouds from a particular symbol created previously? Thanks!
February 13th 2008 07:02AM - Claudio Bueno
i'm doing a project.
how am i able to make a TREE sway, rather grass?
...i'm trying to give it a windy effect that's why.
can i get some help?
i'm doing the seasons and i've got the rain, and snow, all i need is wind.
February 18th 2008 12:02PM - molly
plz i need help i have to the the same thing on the grace but with a flame
April 8th 2008 05:04AM - ahmed
Hi
this is a nice scipt. I have tried it with a bigger fiel, i made changing as below :
Xplots=80;
Yplots=80;
but everything runs extremly slow then, is it bec the calculation of each grass takes to much time? is there a way to run ist faster?
Jason
September 1st 2008 07:09AM - Jason
Add a Question or Suggestion






