当前位置:软件学习 > Flash >>

flash中落花效果AS3实现代码

以下为flash落花效果的as3代码,如要欣赏FLASH动画效果请见本文底部的动画演示。

 代码如下 复制代码

function flower():Sprite {
var mc:Sprite=new Sprite();
var color:uint=Math.random()*0x88ffff+0x880000;
var num:int=int(Math.random()*9+1);
var i:int=0;
while (i++<num) {
var huaBan:Shape=new Shape();
huaBan.graphics.beginGradientFill(GradientType.RADIAL,[0xffffff,color],[0.6,1],[0,255]);
huaBan.graphics.moveTo(0,0);
huaBan.graphics.curveTo(30,-45,0,-50);
huaBan.graphics.curveTo(-30,-45,0,0);
huaBan.rotation=360/num*i;
mc.addChild(huaBan);
}
return mc;
}
//画花函数//www.zzzyk.com
var mcNum:int=50;//花的数量
var mcArr:Array=[];//存储花的数组
var vxArr:Array=[];//x速度数组
var vyArr:Array=[];//y速度数组
var vrArr:Array=[];//旋转速度数组
var clickCount:int=0;//点击次数
for (var i:int=0; i<mcNum; i++) {
var mc:Sprite=flower();
addChild(mc);
mc.scaleX=mc.scaleY=0.2+Math.random()*0.5;
mc.x=Math.random()*stage.stageWidth;
mc.y=-Math.random()*stage.stageHeight;
vxArr.push(Math.random()*2-1);
vyArr.push(Math.random()*2+2);
vrArr.push(Math.random()*4-2);
mcArr.push(mc);
}
addEventListener(Event.ENTER_FRAME,frame);
function frame(e:Event):void {
for (i=0; i<mcNum; i++) {
mcArr[i].x+=vxArr[i];
mcArr[i].y+=vyArr[i];
mcArr[i].rotation+=vrArr[i];
if (mcArr[i].y>stage.stageHeight+mcArr[i].width/2) {
mcArr[i].x=Math.random()*stage.stageWidth;
mcArr[i].y=-Math.random()*stage.stageHeight;
}
}
}
stage.addEventListener(MouseEvent.CLICK,onClick);
function onClick(e:MouseEvent):void {
if (clickCount++%2==0) {
removeEventListener(Event.ENTER_FRAME,frame);
} else {
addEventListener(Event.ENTER_FRAME,frame);
}
}

补充:flash教程,As3.0
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,