Flex 粒子效果
、
话不多说 上代码
自定义了MyEllipse类 随机了粒子的大小 颜色 重力值 和偏移量
public class MyEllipse extends Ellipse { /**重力*/ var gravity:Number=0.1; /**偏移量*/ var offsetX=1; var offsetY=-3; public function MyEllipse() { offsetY=GetRandomNum(-5, 5); offsetX=GetRandomNum(-3, 3); var solidColor:SolidColor=new SolidColor(uint(GetRandomColor()), 1); this.fill=solidColor; var size:Number=GetRandomNum(1, 10); this.width=size; this.height=size; } /**扩散*/ public function Pervasion():void { this.x+=offsetX; this.y+=offsetY; this.alpha+=-gravity / 10; offsetY+=gravity; } /**获取随机颜色*/ public static function GetRandomColor():String { return "0x" + Math.round(Math.random() * 0xFFFFFF).toString(16); } /**随机数*/ public static function GetRandomNum(min:int, max:int):Number { return Math.round(Math.random() * max) + min; } }
呈现界面代码
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" width="500" height="400" minWidth="300" minHeight="600" creationComplete="Init()"> <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> </fx:Declarations> <fx:Script> <![CDATA[ import mx.graphics.GradientEntry; import mx.graphics.RadialGradient; import mx.graphics.SolidColor; import mx.graphics.Stroke; import org.osmf.events.TimeEvent; import spark.primitives.Ellipse; import spark.primitives.Rect; private function Init():void{ mainCanvas.addEventListener(MouseEvent.MOUSE_MOVE,OnMouseClick); var timer:Timer = new Timer(10); timer.addEventListener(TimerEvent.TIMER,OnTimerEvent); timer.start(); } private function OnTimerEvent(event:TimerEvent):void { var clength:int = mainCanvas.numElements; for(var i:int=0;i<clength;i++) { var ellipse:MyEllipse = mainCanvas.getElementAt(i) as MyEllipse; ellipse.Pervasion(); if(ellipse.alpha<=0) { mainCanvas.removeElementAt(i); clength-=1; } } } public function OnMouseClick(event:MouseEvent ):void{ var ellipse:MyEllipse = new MyEllipse(); ellipse.x=mouseX; ellipse.y = mouseY; mainCanvas.addElement(ellipse); } ]]> </fx:Script> <s:BorderContainer id="mainCanvas" left="10" right="10" top="10" bottom="10" backgroundColor="#000000" cornerRadius="10" horizontalCenter="0" verticalCenter="0"> </s:BorderContainer> </s:Application>
最后 感谢http://upload.meqzone.com/ 提供的免费上传swf的服务