Flash AS3.0实现下雪效果源代码
by
at 2010-09-21 14:42:16
original http://www.javaeye.com/topic/769085
效果观看:
http://www.flexedu.com/zuopin/huangdeqiang/flyrain.swf
源代码:
http://www.flexedu.com/zixun_show.php?id=153&xid=3
FlyRain.as:
package
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.filters.BlurFilter;
[SWF(backgroundColor="#000000")]
public class FlyRain extends Sprite
{
//首先要创建出一个Rain的类;
public var rain:Rain;
public var rains:Array;
public var vx:Number=0;
public var vy:Number=0;
public var gravity:Number=0.2;
public var ax:Number=1;
public var count:int ;
public function FlyRain()
{
//设置舞台;
//stage.align=StageAlign.TOP_LEFT;
//stage.scaleMode=StageScaleMode.NO_SCALE;
//创建一个数组用来盛放rain元件;
rains=new Array();
//用一个循环体创建出200个雪花,随即放在舞台上,并增加一个模糊滤镜的效果,显得更加逼真。
for (var i:int=1; i <= 300; i++)
{
//随机设置雪花的大小
rain=new Rain( 1);
addChild(rain);
//建立一个随机模糊的量
//rain.filters=[new BlurFilter(Math.random() * 1 + 3, Math.random() * 1 + 3)];
rain.x=Math.random() * 100 ;
rain.y=-Math.random() * 300 ;
var z:Number = Math.random() ;
rain.z=Math.random() * 2000-1500 ;
//rain.vx=Math.random()2 ;
//rain.vy=Math.random()3 ;
//rain.scaleX=rain.scaleY=z3;
//将创建出的雪花添加进数组
rains.push(rain);
}
addEventListener(Event.ENTER_FRAME, K);
}
private function K(e:Event):void
{
count++;
//if (count % 2)
for (var i:int=0; i < rains.length; i++)
{
//创建一个雪花实例,使他引用数组中的雪花
var rain:Rain=Rain(rains[i]);
//创建出一个变量,用来改变雪花下落的姿态;
vx=Math.random();
vy=Math.random() * 5 + 1 + gravity;
//随即下落
rain.x += (vx + 2);
rain.y += (vy + 2);
//rain.rotationZ += Math.random()5-10
//如果雪花超出舞台那么就重新放回舞台上方
if (rain.y > 400)
{
rain.x=Math.random() * 600;
rain.y=Math.random() ;
}
}
}
}
}
Rain.as:
package{
import flash.display.Sprite;
import flash.filters.GlowFilter;
public class Rain extends Sprite {
public var R:Number;
public function Rain(R:Number=5) {
R=R;
drawing();
}
private function drawing():void {
var n:Number = Math.random() ;
graphics.lineStyle(0.1,0xffffff);
graphics.lineTo(this.x + n + R, this.y+n +R+10);
}
}
}
源代码下载:
http://www.flexedu.com/zuopin/huangdeqiang/rain.rar
<br><br>
作者: <a href="http://wangkuaigan.javaeye.com">wangkuaigan</a>
<br>
声明: 本文系JavaEye网站发布的原创文章,未经作者书面许可,严禁任何网站转载本文,否则必将追究法律责任!
<br><br>
<span style="color:red">
<a href="http://www.javaeye.com/topic/769085" style="color:red">已有 <strong>0</strong> 人发表回复,猛击->><strong>这里</strong><<-参与讨论</a>
</span>
<br><br><br>
JavaEye推荐