阅读文章(首页/网页制作/Flash教程/)

Flash AS动画实例:风吹云飘草动

[日期:2007-11-01] 来源:  作者: [字体: ]

  

 

选择第一帧添加如下代码。

//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));
 }
}

  然后回到主场景中,建立两个图层,下面的为云的图层,上面的为草的图层,这里我绘制了一个放草的框框。

Flash AS 实现好看的清晰的飘动云和风吹草动画

  按Ctrl+L打开库分别把草元件和云元件放到相应的图层。给草命名实例grass。

Flash AS 实现好看的清晰的飘动云和风吹草动画

  最后可以测试影片了!但是好象机器运行速度在减慢啊!祝你好运!



上一页 [1] [2] [3]    
阅读:
录入:blue1000

评论 】 【 推荐 】 【 打印
上一篇:用Flash AS3制作统计饼图动画效果
下一篇:Flash MX软件快捷键整理
相关文章      
本文评论
  老师辛苦就为了教别人,请你给效果图,没有做成功!   (难难 ,2008-09-21 )
  kk   (// ,2008-04-14 )
  不知道什么意思   (肖 ,2008-03-22 )
发表评论


点评: 字数
姓名:

  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款