//=================MC类自定义方法====================
//——————宽高矩形法,根据初始点坐标和宽与高确定矩形。x,y为初始点坐标,kuan和gao为矩形的宽和高。
MovieClip.prototype.kuangaojuxing = function(x, y, kuan, gao, bangzhi, yanse, toumingdu) {
this.lineStyle(bangzhi, yanse, toumingdu);
this.moveTo(x, y);
this.lineTo(x+kuan, y);
this.lineTo(x+kuan, y+gao);
this.lineTo(x, y+gao);
this.lineTo(x, y);
};
//——————MC定位方法
MovieClip.prototype.dingwei = function(x, y) {
this._x = x;
this._y = y;
};
//==================创建下雨动画=====================
//定义下雨动画函数,相关参数请参考下面的注释
/*
x_fc:下雨动画的X坐标
y_fc:下雨动画的Y坐标
yanse_fc:雨滴颜色
toumingdu_fc:雨滴透明度
yudikuan_fc:雨滴的宽
yudigao_fc:雨滴的高
qingxie_fc:雨滴的倾斜度
mijidu_fc:雨滴的密集度
pinlu_fc:下雨画面更换的频率
kuan_fc:下雨画面宽
gao_fc:下雨画面高(它和上面的参数一起控制下雨范围)
*/
function xiayudonghua(x_fc, y_fc, yanse_fc, toumingdu_fc, yudikuan_fc, yudigao_fc, qingxie_fc, mijidu_fc, pinlu_fc, kuan_fc, gao_fc) {
//创建下雨动画MC
createEmptyMovieClip("xiayu_mc", 10000);
//定位下雨动画MC
xiayu_mc.dingwei(x_fc, y_fc);
//在下雨动画里创建单个雨滴MC
xiayu_mc.createEmptyMovieClip("yudi_mc", 10000);
with (xiayu_mc) {
//宽高矩形法绘制单个雨滴MC
yudi_mc.beginFill(yanse_fc, toumingdu_fc);
yudi_mc.kuangaojuxing(0, 0, yudikuan_fc, yudigao_fc, 0, 0x000000, 0);
yudi_mc.endFill();
//定义雨滴的倾斜度
yudi_mc._rotation = qingxie_fc;
//连续调用“jingtaihuamian”函数,形成下雨动画
var xiayu_si = setInterval(jingtaihuamian, pinlu_fc, mijidu_fc, kuan_fc, gao_fc);
}
}
//定义下雨动画的静态画面函数
function jingtaihuamian(mijidu_fc, kuan_fc, gao_fc) {
//随机复制单个雨滴MC,形成单贞下雨画面
for (var i = 0; i<mijidu_fc; i++) {
xiayu_mc.yudi_mc.duplicateMovieClip("yudi_mc"+i, i);
xiayu_mc.yudi_mc.dingwei(random(kuan_fc), random(gao_fc));
}
}
//==============按钮控制下雨动画===============
kaishi_btn.onRelease = function() {
//在需要下雨的贞调用这个函数就可以下雨了!
xiayudonghua(0, 0, 0xffffff, 50, 2, 30, 30, 100, 10, 550, 400);
};
tingzhi_btn.onRelease = function() {
//天晴的时候就调用这个函数
xiayu_mc.removeMovieClip();
};
★只要把上面的代码复制到第一贞,然后再建立一个“kaishi_btn”和"tingzhi_btn”按钮就可以欣赏效果了。我把代码写到函数里,是为了方便在动画中使用,在需要下雨的地方,只要调用"xiayudonghua"这个函数就可以了,而在天晴的时候,写一句xiayu_mc.removeMovieClip()就OK了,怎么样?挺好用的吧
★下面大致讲一下原理。