Newest Articles

MegaCombs
Flash Media Player
XML Driven Pie Chart
Base Defender
Hangman Game
8 Ball Pool


Popular Articles

True Fullscreen Flash Mode
Mp3 Player with XML Playlist
Image Slider
Flash Media Player
3d Rotating Image Cube
FLV Player


Random Articles

Image Slider with Easing
Album Slide
Flash Media Player
True Fullscreen Flash Mode
Growing Tree using Recursion
LED Text Scroller


Links

Foundation-Flash
MickM
TutorialQuest
Tutorialsphere.com - Free Online Tutorials
Newgrounds
TWiT
Link to SwfSpot
Swf Spot



rss feed

Moving Clouds and Waving Grass

Moving Clouds and Waving Grass
AddThis Social Bookmark Button
Description: Add puffy clouds and wavy grass to your movies
Author: John Bezanis
Added: May 23rd 2007
Version: Flash 8


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:
  1. //Number of clouds
  2. clouds=6;
  3. //These are just general boundaries.
  4. //To use exact boundaries, create a mask in the parent level of the size desired
  5. //Height of the sky
  6. skyheight=Stage.height;
  7. //Width of the sky
  8. skywidth=Stage.width;
  9. //Max size of a cloud
  10. cloudsize=300;
  11. //Amount of blur applied to the shapes to make them cloud-like
  12. blursize=40;
  13. //Clouds move at a random speed. this is the minimum speed
  14. cloudminspeed=.5;
  15. //Variance in speed from cloud to cloud
  16. cloudspeedvariance=1;
  17. //Create the clouds
  18. for(c=1;c<=clouds;c++){
  19. //create an empty movie clip to hold the cloud
  20.   this.createEmptyMovieClip("cloud"+c,this.getNextHighestDepth());
  21. //generate a cloud. Pass in the instance name of the newly created cloud
  22.   shapecloud("cloud"+c);
  23. //Set the x position to a random position within the boundaries
  24.   eval("cloud"+c)._x=Math.random()*skywidth-eval("cloud"+c)._x/2;
  25. //Set the y position to a random position within the boundaries
  26.   eval("cloud"+c)._y=Math.random()*(skyheight)-eval("cloud"+c)._height;
  27. }
  28. //Run at the start of each frame
  29. onEnterFrame=function(){
  30. //Run for each cloud
  31.   for(c=1;c<=clouds;c++){
  32. //Move the cloud to the left according to its speed
  33.     eval("cloud"+c)._x-=eval("cloud"+c).cloudspeed;
  34. //If the cloud is past the stage to the left, reset it to the right. Create a new shape and color  
  35.     if(eval("cloud"+c)._x+(eval("cloud"+c)._width/2)+cloudsize<0){
  36. //Reset the x position
  37.       eval("cloud"+c)._x=skywidth;
  38. //Reshape and recolor the cloud
  39.       shapecloud("cloud"+c);
  40.     }
  41.   }
  42. }
  43. //This function creates the shape and color of a cloud
  44. function shapecloud(cloudid){
  45. //Clear the current contents of the cloud
  46.   eval(cloudid).clear();
  47. //Set the new shade between 224 and 255. This number is used for the red, green, and blue, to create a grayscale color
  48.   cloudcolor=Math.round(Math.random()*31)+224;
  49. //Use no line
  50.   eval(cloudid).lineStyle(undefined, (cloudcolor+cloudcolor*0x100+cloudcolor*0x10000), 100, false, "none", "none", "none", 1);
  51. //Set the fill color. cloudcolor is used 3 times, for red, green, and blue
  52.   eval(cloudid).beginFill((cloudcolor+cloudcolor*0x100+cloudcolor*0x10000));
  53. //Set a starting coordinate for the cloud 
  54.   eval(cloudid).moveTo(Math.random()*cloudsize,Math.random()*cloudsize);
  55. //Draw an invisible line to another point the combined lines form shapes, which are the clouds.
  56. //They don't look much like clouds until the blur is applied
  57.   eval(cloudid).lineTo(Math.random()*cloudsize,Math.random()*cloudsize);
  58.   eval(cloudid).lineTo(Math.random()*cloudsize,Math.random()*cloudsize);
  59.   eval(cloudid).lineTo(Math.random()*cloudsize,Math.random()*cloudsize);
  60.   eval(cloudid).lineTo(Math.random()*cloudsize,Math.random()*cloudsize);
  61.   eval(cloudid).lineTo(Math.random()*cloudsize,Math.random()*cloudsize);
  62. //Apply a blur to the shape
  63.   eval(cloudid).filters = [new flash.filters.BlurFilter(blursize,blursize,2)];
  64. //Set a new cloud speed
  65.   eval(cloudid).cloudspeed=Math.random()*cloudspeedvariance+cloudminspeed; 
  66. }
Code for the grass:
  1. //Height of each blade of grass
  2. grassheight=35;
  3. //Average space in between each blade of grass
  4. grassspacing=5;
  5. //Maximum sway of each blade of grass
  6. maxsway=20;
  7. //Number of blades of grass along the x axis
  8. xplots=30;
  9. //Number of blades of grass along the y axis
  10. yplots=20;
  11. //The wind has an x position and the grass is attracted to the position
  12. windxpos=0;
  13. //Velocity of the wind left and right
  14. windspeed=0;
  15. //Gives the grass a bent effect. The grass bends 1/4 of the way up
  16. grasscontrol=grassheight/4;
  17. //Array containing the info for each blade of grass
  18. grasscoords=[];
  19. //These loops go through the field, planting each blade of grass
  20. for (xpos=0; xpos<xplots; xpos++) {
  21.   for (ypos=0; ypos<yplots; ypos++) {
  22.     //x position, y position, sway, and color
  23.     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]);
  24.   }
  25. }
  26. //Run on each frame
  27. onEnterFrame=function(){
  28. //Clear all of the grass so it can be redrawn with different sway
  29.   this.clear();
  30. //Change the speed of the wind
  31.   windspeed=Math.max(-50,Math.min(50,windspeed+Math.random()*40-20));
  32. //Move the position the blades are attracted according to the windxpos
  33.   windxpos+=windspeed;
  34. //If the windxpos moves too far to the left, reverse its speed
  35.   if(windxpos<-100){
  36.     windxpos=-100;
  37.     windspeed*=-1;
  38.   }
  39. //If the windxpos moves too far to the right, reverse its speed
  40.   else if(windxpos>grassspacing*xplots+100){
  41.     windxpos=grassspacing*xplots+100;
  42.     windspeed*=-1;
  43.   }
  44. //handle the redraw for each blade of grass
  45.   for(coord=0;coord<grasscoords.length;coord++){
  46. //Set the line style. 0 means use hairline width and grasscoords[coord][3] is the color    
  47.     this.lineStyle(0, grasscoords[coord][3], 100, false, "normal", "none", "none", 1);
  48. //Adjust the sway according to the grass's current sway and the windxpos
  49.     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);
  50. //Move to the base of the blade of grass
  51.     this.moveTo(grasscoords[coord][0],grasscoords[coord][1]);
  52. //Draw a curved line to the new top of the blade of grass
  53.     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));
  54.   }
  55. }

Download the source file below:

Download Source File
Comments
hi guys
June 11th 2007 04: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 05:07PM   -   ME!
//Number of blades of grass along the y axis
yplots=20;
skyheight=Stage.height;
August 18th 2007 01: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 07: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
a good example. thanks!
October 10th 2008 03:10PM   -   vasya
great rain/snow code from 'ME'!!
January 16th 2009 07:01AM   -   nike
have i done somethin wrong??
i made 2 movie clips, one with instance name of grass and one with clouds, gave them a linkage to export it for action script, i put the clouds on the main scene, and the grass, but its coming up with lots of errors? wat am i missing?
February 7th 2009 05:02AM   -   George
i tried to change the direction of the clouds from y+ to y-, but it didnt work, it only past once.. can anyone tell how to do it?
February 26th 2009 08:02PM   -   borchie
loop it with -double y *60
April 4th 2009 05:04AM   -   pelpw
Very Good Script....................
April 7th 2009 01:04AM   -   isura
great script wish it was in as3.0 though
May 4th 2009 06:05PM   -   johno
Great flash movie however flash is dying as far as website is concerned Google needs to come up with a better way to index flash websites.

August 26th 2009 04:08PM   -   furniture packages Spain
This is very good script. Thanks
August 31st 2009 05:08PM   -   Vintage dress wedding
This is very nice script. Thank you very much
August 31st 2009 05:08PM   -   fred olsen cruises
Cool little script. You wouldn't believe how long I've been watching those clouds go across the screen seeing if I can spot two the same!!
September 1st 2009 11:09AM   -   Electric fireplaces
It is a excellent script. Very nice find. Thank you very much.
September 2nd 2009 01:09PM   -   fetal doppler
Good script, thanks.
September 18th 2009 08:09AM   -   online games
Great work..Thanks a lot for sharing this..
September 26th 2009 09:09AM   -   yoga exercises online
Great script..Much appreciated
September 26th 2009 09:09AM   -   Laptop Reviews
Also suggests a potential special effect--for powerful attacks, have the grass "wave" away from the attack. Instant visual confirmation that an attack was a powerhouse.
September 27th 2009 07:09AM   -   movie download links
This is very good script. Thanks
September 29th 2009 11:09PM   -   mevric
Very cool graphics. Terrific work with this design.
October 13th 2009 08:10AM   -   Massachusetts Real Estate
Actually that is really clever, I would never have thought about scripting it this way. Appreciate this a lot.
October 13th 2009 08:10AM   -   zotrim online
Great tutorial and thanks for sharing the script you have made it very easy to understand by using the commenting.
October 14th 2009 05:10AM   -   Amber
Great tutorial and thanks for sharing the script you have made it very easy to understand by using the commenting.
October 14th 2009 05:10AM   -   Amber
Very nice animation. Thank you for providing the script, I would like also to thank you for the detailed commentaries. It makes the script so much easier to understand.
November 6th 2009 06:11AM   -   Jobless data
These were some really nice flash graphics. Pleasing to the eyes.
November 17th 2009 10:11PM   -   Make Money Online
This was a rather interesting. Keep up the awesome photos.
November 17th 2009 10:11PM   -   Web Hosting Reviews
Really nice post,thanks for sharing....
November 21st 2009 10:11AM   -   Free online games
Students in

the world order the written research papers or <a href="http://www.exclusivepapers.com">custom writing</a> at the essay writing organization just about

this good topic. Students heard about the <a href="http://www.exclusivepapers.com">essay

writing</a> from the essay writing services.
November 22nd 2009 06:11AM   -   ridons
#
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
November 25th 2009 01:11PM   -   Voucher Codes
Every good blog has two features, they have a very good topic and you see lot of good comments. Its blogging that has made me know new things everyday. I absolutely sure that people in this world have come closer in their personal world. There so many things working for a blog and the topic. Its just a flat world now because of blogs like this one, I appreciate it.
December 3rd 2009 11:12AM   -   Dental implants Birmingha
This is very cool.
December 3rd 2009 02:12PM   -   Stocks For Dummies
I love watching the grass go back and forth.
December 3rd 2009 02:12PM   -   Work From Home
What a nice animation. Thank you.
December 5th 2009 07:12AM   -   work at home
This is very good script. Thanks
December 6th 2009 01:12AM   -   seo
Thank you for providing the script, I would like also to thank you for the detailed commentaries. It makes the script so much easier to understand.
December 7th 2009 04:12PM   -   Jobs For College Students
I want to use hit test to make my character not move when it rich the walls.
December 10th 2009 05:12AM   -   club penguin
I have bookmarked your site. Thanks for sharing
December 13th 2009 08:12AM   -   dental
I have bookmarked it also. Thanks
December 17th 2009 10:12AM   -   Debt Relief Grants
this is so good to see. keep up the good work
December 17th 2009 12:12PM   -   orlando porcelain veneers
Believe me, there are days when I seriously consider automatic faucet and also trust me, contemporary automatic faucets pays off big time in the end. There is a note of concern about that and I'm not one of those sensor faucet snobs. That topic has been beaten to death. We have all heard about ir sensor faucet before. This is how to stop worrying and enjoy your automatic faucet. Where can these people expose the best sensor faucet tips? Even if this proves for others, your motion sensor faucet may wildly different way. In this way, you can experience your true contemporary automatic faucets passion. Some, even more. We must reamin vigilant. I'm always well groomed. Man, time flies when your busy with your ir sensor faucet. This is a mammoth situation. Well, have a nice day. You need to understand the importance of contemporary automatic faucets. Make sure that you leave yourself plenty of time. This was suggested by a friend of mine. This should be a good lead in for this info. Contemporary automatic faucets can be quite fierce in that situation and also let me start out by saying that I have nothing against sensor faucet. Now is the time to read my completely off-base thoughts concerning automatic faucet.
December 19th 2009 03:12PM   -   Automatic Faucets
thanks for giving the link to the source file
December 28th 2009 10:12AM   -   improve memory
thanks a lot
December 28th 2009 10:12AM   -   90 day loans
This is a great script, loved it, kep developping a teaching flash.
December 30th 2009 07:12AM   -   ganar dinero encuestas
You always have great information on your site . Enjoy very much returning. I now have you bookmarked!
December 31st 2009 09:12PM   -   electronic cigarette
This was suggested by a friend of mine. This should be a good lead in for this info.
January 1st 2010 03:01PM   -   Indonesia Java Internatio
i must course in here
January 1st 2010 03:01PM   -   Astaga.com lifestyle on t
just awesome and perfect thanks
January 3rd 2010 10:01PM   -   total_broke
i like
January 4th 2010 01:01AM   -   cheap wedding dresses
Wow, the code really helps. Thanks a lot.
January 4th 2010 04:01PM   -   cortney
i must course in here
January 5th 2010 03:01PM   -   ncis: los angeles season
just awesome and perfect thanks
January 5th 2010 03:01PM   -   better off ted season 2
Wow, the code really helps. Thanks a lot.
January 5th 2010 03:01PM   -   bad girls club season 4
simple code for an awesome effect. Lots to play around with here, thanks.
January 7th 2010 06:01AM   -   herbal phentermine
thanks
January 14th 2010 10:01AM   -   projeksiyon
Now this can be cool on many different sites and for different reasons.
January 20th 2010 08:01PM   -   NY Minute Dating
Nice green movings grass
January 21st 2010 01:01PM   -   grass wallpapers
Thank you.
January 22nd 2010 04:01PM   -   acyclovir 400 mg
Thank you.
January 23rd 2010 04:01PM   -   Cialis side effect
Great information you have here on your site!
January 25th 2010 04:01PM   -   firelight e cigarettes
Special thank for good post.
February 5th 2010 08:02AM   -   à¹‚หลดเพลงà
Thank for the information
February 6th 2010 06:02AM   -   à¹‚หลดเพลงm
Special thank for good post.
February 10th 2010 03:02AM   -   à¹‚หลดเพลงà
Great tutorial for web designers, thanks a lot.
February 11th 2010 08:02PM   -   Jay designer
Always an interesting read here. I know have your site bookmarked and look forward in returning. keep up the interesting posts!
February 14th 2010 04:02PM   -   Home Theater Installation
Hello everyone. The ornament of a house is the friends who frequent it. Help me! Please help find sites for: Hair loss home remedies. I found only this - <a href="http://www.moebel-rau.de/Members/HairLoss/independent-review-hair-loss-product">independent review hair loss product</a>. She ran him for an mental look, hair loss. Hair loss, gender loss in the tips of the internet within the solder fillet can fortunately grow. Best regards :-), Paige from Latvia.
February 22nd 2010 08:02AM   -   Paige
This is cool and helpful. thanks for the tutorial
March 6th 2010 04:03AM   -   Twitter Designs
Add a Comment
name:
website (optional):
captcha type the characters into the box
message (5000 characters or less):