Flash鼠标拖尾效果——粒子
之前有个一种鼠标拖尾效果,这里以粒子的方式再来实现一遍:
1、新建fla文件mouse_drag_tail.fla
2、新建一as文件,粒子类Particles.as,就是一个以不同速度,角度,颜色,半径的圆
3、新建一文档类Main.as,与fla文件关联
Particles.as:
package
{
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
public class Particles extends Sprite
{
private var color:uint;
private var speed:Number;
private var i:Number=0;
private var rad:Number;
private var boxPar:Shape;
public function Particles(_color:uint,_speed:Number,_rad:Number)
{
color=_color;
speed=_speed;
rad=_rad;
addParicle();
this.addEventListener(Event.ENTER_FRAME,enterFrame);
}
//创建粒子(形状、颜色)
private function addParicle()
{
boxPar=new Shape;
addChild(boxPar);
boxPar.graphics.beginFill(color , 1);
boxPar.graphics.drawCircle(0,0,rad);
boxPar.graphics.endFill();
}
//位置变化、透明度
private function enterFrame(e:Event):void
{
boxPar.x += speed;
boxPar.alpha -= 0.05;
if (boxPar.alpha < 0.01)
{
this.removeEventListener( Event.ENTER_FRAME, enterFrame );
parent.removeChild(this);
}
}
}
}
Main.as:
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
public class Main extends Sprite
{
private var maxNum:int=3;
public function Main()
{
init();
}
public function init()
{
this.graphics.beginFill(0x0,1);
this.graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);
this.graphics.endFill();
stage.addEventListener( MouseEvent.MOUSE_MOVE, moveHandler);
}
private function moveHandler(e:MouseEvent):void
{
for (var i = 0; i < maxNum; i++)
{
var color:uint = Math.random()*0xffffff;
var particles=new Particles(color,1,Math.random()*5);
addChild( particles );
particles.x=mouseX;
particles.y=mouseY;
particles.rotation = Math.random() * 360;
}
}
}
}
Ctrl+Enter导出效果如下: