Fireflies
use mouse.. :)
/**
* Copyright Caiim. ( http://wonderfl.net/user/Caiim. )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/qtkV
*/
package {
import flash.events.MouseEvent;
import flash.filters.BlurFilter;
import flash.filters.GlowFilter;
import flash.events.Event;
import flash.display.Graphics;
import flash.display.MovieClip;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.display.Sprite;
[SWF(width=500,height=500,backgroundColor=0x000000,frameRate=30)]
public class FlashTest extends Sprite {
// number of background fireflies
public var num:int = 120;
// number of following fireflies
public var followNum:int = 50;
// timer to spawn the background fireflies
public var timer:Timer = new Timer(100);
public function FlashTest() {
timer.addEventListener(TimerEvent.TIMER, spawnFirefly);
timer.start();
// spawns the following fireflies
for(var i:int = 0;i<followNum;i++)
{
var follow_mc:MovieClip = createFirefly(0xFFFBB6);
follow_mc.acceleration = 0.5;
follow_mc.speedX = follow_mc.speedY = 0;
follow_mc.speedLimit = 2+Math.random()*10;
follow_mc.addEventListener(Event.ENTER_FRAME, followMouse);
stage.addChild(follow_mc);
}
}
// function to follow mouse
public function followMouse(e:Event):void
{
var _mc:MovieClip = MovieClip(e.target);
if(_mc.alpha<1)
_mc.alpha+=0.01;
if(_mc.x < mouseX)
_mc.speedX += _mc.acceleration;
else
_mc.speedX -= _mc.acceleration;
if(_mc.speedX > _mc.speedLimit)
_mc.speedX = _mc.speedLimit;
else if(_mc.speedX < -_mc.speedLimit)
_mc.speedX = -_mc.speedLimit;
if(_mc.y < mouseY)
_mc.speedY += _mc.acceleration;
else
_mc.speedY -= _mc.acceleration;
if(_mc.speedY > _mc.speedLimit)
_mc.speedY = _mc.speedLimit;
else if(_mc.speedY < -_mc.speedLimit)
_mc.speedY = -_mc.speedLimit;
_mc.x += _mc.speedX;
_mc.y += _mc.speedY;
// slows down if gets too near to the mouse
if(Math.abs(_mc.x - mouseX) < 50)
_mc.speedX *= 0.99;
if(Math.abs(_mc.y - mouseY) < 50)
_mc.speedY *= 0.99;
}
// to create a new firefly
public function createFirefly(c:uint):MovieClip
{
var _mc:MovieClip = new MovieClip();
var g:Graphics = _mc.graphics;
g.beginFill(c);
g.drawCircle(0, 0, 0.1+Math.random());
g.endFill();
_mc.alpha = 0;
_mc.x = Math.random()*stage.stageWidth;
_mc.y = Math.random()*stage.stageHeight;
_mc.glow = 5+Math.random()*5;
_mc.c = c;
_mc.filters = [new BlurFilter(2,2, 3)];
_mc.addEventListener(Event.ENTER_FRAME, glowing);
// function to clean up the firefly
_mc.cleanUp = function():void{
_mc.removeEventListener(Event.ENTER_FRAME, glowing);
}
return _mc;
}
// makes the firefly glows
public function glowing(e:Event):void
{
var _mc:MovieClip = MovieClip(e.target);
if(Math.random()<0.1)
_mc.glow += (Math.random()-0.5)/3;
if(_mc.glow < 1)
_mc.glow = 1;
if(_mc.glow > 5)
_mc.glow = 5;
var glowF:GlowFilter = new GlowFilter(_mc.c,1, _mc.glow*2/3, _mc.glow*2/3, _mc.glow, 3);
_mc.filters = [glowF];
}
// to spawn the background firefly
public function spawnFirefly(e:TimerEvent):void
{
if(stage.numChildren < num)
{
var _mc:MovieClip = createFirefly(0xFDF9A3+Math.random()*0x000222);
_mc.angle = 2*Math.PI*Math.random();
_mc.addEventListener(Event.ENTER_FRAME, fly);
stage.addChild(_mc);
}
}
// to make the background firefly flies.. :p
public function fly(e:Event):void
{
var _mc:MovieClip = MovieClip(e.target);
_mc.x += Math.cos(_mc.angle);
_mc.y += Math.sin(_mc.angle);
if(Math.random()<0.1)
_mc.angle += (Math.random()-0.5);
if(_mc.alpha<1)
_mc.alpha+=0.01;
if(_mc.x < 0 || _mc.x > stage.stageWidth || _mc.y < 0 || _mc.y > stage.stageHeight)
{
_mc.cleanUp();
_mc.removeEventListener(Event.ENTER_FRAME, fly);
stage.removeChild(_mc);
}
}
}
}