纯ActionScript实现的飘雪动画

solar @ 2009年02月21日 - 唉,你来晚一步,前 3 排已经被占啦 » - 495 Views

这是纯ActionScript编写的飘雪动画,代码注释如下:

var sceneWidth =550;//设置场景的宽
var sceneHeight=300;/设置场景的高
var sideDisWidth = Stage.width/2-sceneWidth/2;//定义变量,雪的横向位移范围
var sideDisHeight = Stage.height/2-sceneHeight/2;//定义变量,雪的纵向位移范围
var snowNum = 80;//雪的数量

var snowSpace:MovieClip = _root.createEmptyMovieClip("room", 1);//声明一个空影片剪辑snowSpace. function addMasker() {//声明addMasker函数,执行两项工作.1是画外边框,2是画雪景 //———–这是完成的第一项工作,画舞台外边框———–
_root.createEmptyMovieClip("masker", -2);//创建空影片剪辑masker,层深-2.任务是为舞台画外边框,并填充颜色
with (masker) {//设置masker的属性。
lineStyle(1, 0xcccccc, 100);//线条颜色
beginFill(0×999999, 100);//绘制矩形闭合路径,并填充灰色,透明度为100.
moveTo(sideDisWidth, sideDisHeight);//绘制起点
lineTo(sideDisWidth+sceneWidth, sideDisHeight);//绘制的右上角点的位置
lineTo(sideDisWidth+sceneWidth, sideDisHeight+sceneHeight);//绘制的右下角点的位置
lineTo(sideDisWidth, sideDisHeight+sceneHeight);//绘制的左下角点的位置
lineTo(sideDisWidth, sideDisHeight);//绘制的左上角点的位置
endFill();//结束绘制
}
//———–第一项任务结束—————————-
//———–把第二项画雪景工作交给createSnow———–
createSnow();
}
//*****下面就是画雪景了,分了4步:createSnow雪外观,drawSnow画雪,snowProperty一个雪的下降,snowFall整个雪景的位移范围***** function createSnow() {//定义一个匿名函数,完成声明一个雪MC,雪半径,循环画雪
var n = 0;//局部变量n=0
while (nvar snow:MovieClip = snowSpace.createEmptyMovieClip("s"+n, n);//声明snowSpace剪辑由"s"+n个snow组成
var radius:Number = Math.random()*3;//雪半径大小
drawSnow(snow, radius);//把snow, radius把参数传递给drawSnow函数
n++;
}
} function drawSnow(snow:MovieClip, radius:Number) {//定义一个匿名函数,画一个雪
var p = radius*.9;//局部变量P,用来定义雪大小的坐标值
with (snow) {//设置snow这个对象的动作.画雪圆形白色填充
colors = [0xCCCCCC, 0xFFFFFF];//雪的颜色
alphas = [100, 100];//透明度
ratios = [0, 255];//采样处的宽度的百分比
matrix = {matrixType:"box", x:-Math.random()*2*radius, y:-Math.random()*2*radius, w:2*radius, h:2*radius, r:(90/180)*Math.PI};//一个变形矩阵
beginGradientFill("radial", colors, alphas, ratios, matrix);// 填充色.
curveTo(p, -p, radius, 0);//定义点坐标,画雪
curveTo(p, p, 0, radius);
curveTo(-p, p, -radius, 0);
curveTo(-p, -p, 0, -radius);
endFill();//结束
}
snowProperty(snow, sceneWidth, sceneHeight);//把参数传递给snowProperty
} function snowProperty(snow, w, h) {//定义一个匿名函数,一个雪如何下降
snow._x = sideDisWidth+Math.random()*w;//根据可编辑的宽及0和1中的任意一个数乘雪宽计算雪的X轴起始位置
snow._y = sideDisHeight+Math.random()*h;//根据可编辑的高及0和1中的任意一个数乘雪高计算雪的Y轴起始位置
snow._rotation = Math.random()*120+30;//设置旋转角度.有立体感.雪的旋转程度为0和1中的任意一个数乘120+30
snow.stepX = Math.cos((snow._rotation*Math.PI)/180);//雪横向位移.用计算余弦(旋转角度*常数3.14)/180)
snow.stepY = Math.random()*2+1;//雪纵向位移.为0和1中的任意一个数*2-1
setInterval(snowFall, 30, snow);//每隔0.5秒调用一次,由snowFall完成
} function snowFall(snow) {//定义一个匿名函数,该函数为大雪降落位移范围
snow._x += snow.stepX;//横向下落位移
snow._y += snow.stepY;//纵向下落位移
if (snow._xsnow._x = sideDisWidth+sceneWidth;//为雪的横坐标附值
}
if (snow._x>sideDisWidth+sceneWidth) {//如果雪的横向坐标大于所附的值
snow._x = sideDisWidth;//横向位移再回到初始点
}
if (snow._y>sideDisHeight+sceneHeight) {//如果雪的纵向坐标大于所附的值
snow._y = sideDisHeight;//纵向位移再回到初始点
}
}
addMasker();
//———–第二项任务完成———–
//下面是签名
_root.createEmptyMovieClip("myMC", 10);//创建作为现有影片剪辑子级的空影片剪辑myMC,层深10
with (myMC) {//影片剪辑myMC的属性,在层深10的雪的属性
var infor:TextField = myMC.createTextField("infor", 1, sideDisWidth+sceneWidth-100, sideDisHeight+sceneHeight, 100, 20);//创建文本字段并附给infor
infor.autoSize="right";//文本字段的自动大小调整和对齐
infor.selectable=false;//文本字段是否可编辑。false为不可编辑
infor.textColor = 0×999999;//文本颜色

  1. i=1 xue._visible=0 _root.onEnterFrame=function(){ if(i<100){ xue.duplicateMovieClip("xue"+i,i) _root["xue"+i]._x=random(450) _root["xue"+i]._y=xue._y _root["xue"+i]._alpha=random(90)+10 i++ } } var sceneWidth =800; var sceneHeight=330; var sideDisWidth = Stage.width/2-sceneWidth/2; var sideDisHeight = Stage.height/2-sceneHeight/2; var snowNum = 80; var snowSpace:MovieClip = _root.createEmptyMovieClip("room", 1); function addMasker() { _root.createEmptyMovieClip("masker", 10000); with (masker) { beginFill(0x000550, 100); moveTo(sideDisWidth, sideDisHeight); lineTo(sideDisWidth+sceneWidth, sideDisHeight); lineTo(sideDisWidth+sceneWidth, sideDisHeight+sceneHeight); lineTo(sideDisWidth, sideDisHeight+sceneHeight); lineTo(sideDisWidth, sideDisHeight); endFill(); } snowSpace.setMask(masker); createSnow(); } function snowProperty(snow, w, h) { snow._x = sideDisWidth+Math.random()*w; snow._y = sideDisHeight+Math.random()*h; snow._rotation = Math.random()*120+30; snow.stepX = Math.cos((snow._rotation*Math.PI)/180); snow.stepY = Math.random()*2+1; setInterval(snowFall, 30, snow); } function drawSnow(snow:MovieClip, radius:Number) { var p = radius*.9; with (snow) { colors = [0xFFFFFF, 0xFFFFFF]; alphas = [20, 30]; ratios = [0, 255]; matrix = {matrixType:"box", x:-Math.random()*2*radius, y:-Math.random()*2*radius, w:2*radius, h:2*radius, r:(90/180)*Math.PI}; beginGradientFill("radial", colors, alphas, ratios, matrix); moveTo(0, -radius); curveTo(p, -p, radius, 0); curveTo(p, p, 0, radius); curveTo(-p, p, -radius, 0); curveTo(-p, -p, 0, -radius); endFill(); } snowProperty(snow, sceneWidth, sceneHeight); } function createSnow() { var n = 0; while (nsideDisWidth+sceneWidth) { snow._x = sideDisWidth; } if (snow._y>sideDisHeight+sceneHeight) { snow._y = sideDisHeight; } } addMasker(); _root.createEmptyMovieClip("myMC", 10); with (myMC) { lineStyle(1, 0xcccccc, 100); moveTo(sideDisWidth, sideDisHeight); lineTo(sideDisWidth+sceneWidth, sideDisHeight); lineTo(sideDisWidth+sceneWidth, sideDisHeight+sceneHeight); lineTo(sideDisWidth, sideDisHeight+sceneHeight); lineTo(sideDisWidth, sideDisHeight); var infor:TextField = myMC.createTextField("infor", 1, sideDisWidth+sceneWidth-100, sideDisHeight+sceneHeight, 100, 20); infor.autoSize="right"; infor.selectable=false; infor.textColor = 0xcccccc; infor.text = ""; } _root.createEmptyMovieClip("bg", -1); with (bg) { beginFill(0x999999, 100); moveTo(0, 0); lineTo(Stage.width, 0); lineTo(Stage.width, Stage.height); lineTo(0, Stage.height); lineTo(0, 0); endFill(); }

如何使用?

用Flash新建一个文件(废话-_-!),把上面的ActionScript复制到第一帧就OK了!如果你搞不定,下面提供源文件下载。

看效果

点击这里下载源文件

3条 评论 在 “纯ActionScript实现的飘雪动画” 之后

  1. @泰奧朵拉: 原来的域名到期了,续费要100块,后来在新网看到.com两年才93块,索性重新注册了。

    回复这条留言 - 引用

  2. :em02:

    回复这条留言 - 引用

  3. 换了域名,换了版子。很好。
    雪,我喜欢。不过冬天过去了。明年冬天我用上,这篇我收藏了。

    回复这条留言 - 引用

注意:欢迎任何评论,但管理员保留编辑、修改与删除的权利。

在IE里可能无法发表评论(原因不明,囧~~~),请使用 Opera 或 Firefox ,或者其他任何非IE浏览器!


  • :em02:
  • :em11:
  • :em15:
  • :em14:
  • :em08:
  • :em21:
  • :em20:
  • :em04:
  • :em16:
  • :em13:
  • :em09:
  • :em18:
  • :em10:
  • :em19:
  • :em06:
  • :em01:
  • :em05:
  • :em03:
  • :em22:
  • :em07:
  • :em12:
  • :em17:


(Ctrl + Enter 快速提交)