Hello, World! Particle
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.filters.BlurFilter;
import flash.geom.Point;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextLineMetrics;
import flash.utils.Timer;
[SWF(backgroundColor="#FFFFFF", frameRate=60)]
public class Main extends Sprite
{
private const MSG:String = "Hello, world!";
private const FNT:String = "Arial";
private var _particles:Array = [];
private var _nParticles:int;
private var _timer:Timer;
public function Main()
{
// create TextField
var tf:TextField = new TextField();
tf.defaultTextFormat = new TextFormat(FNT, 10);
tf.text = MSG;
// draw the TextField on a Bitmap
var bd:BitmapData = new BitmapData(tf.height, tf.width);
bd.draw(tf);
var i:int;
var j:int;
var sp:Sprite;
for (i = 0; i < bd.width; i++) {
for (j = 0; j < bd.height; j++) {
if (bd.getPixel(i, j) < 0x6a6a6a) {
sp = new Sprite();
sp.graphics.beginFill(0x333333);
sp.graphics.drawCircle(0, 0, 3);
sp.graphics.endFill();
addChild(sp);
_particles.push(new Particle(i, j, sp));
}
}
}
_nParticles = _particles.length;
trace(_nParticles, " particles created.");
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
_timer = new Timer(50);
_timer.addEventListener(TimerEvent.TIMER, timerHandler);
}
private function timerHandler(e:TimerEvent):void
{
Particle.increaseMouseGravity();
}
private function mouseDownHandler(e:MouseEvent):void
{
_timer.start();
}
private function mouseUpHandler(e:MouseEvent):void
{
_timer.stop();
Particle.setMouseGravityToDefault();
}
private function mouseMoveHandler(e:MouseEvent):void
{
Particle.setMousePosition(e.stageX, e.stageY);
}
private function enterFrameHandler(e:Event):void
{
for (var i:int = 0; i < _nParticles; i++)
_particles[i].move();
}
}
}
import flash.display.Sprite;
class Particle {
private var _sp:Sprite;
private var _originalX:int;
private var _originalY:int;
private var _x:Number = 0;
private var _vx:Number = 0;
private var _y:Number = 0;
private var _vy:Number = 0;
private var _u:Number;
private var _v:Number;
private var _l:Number;
private static const K:Number = 0.2;
private static const A0:Number = 2 * 0.4 * K;
private static const A1:Number = K * K;
private static var _L:int = 100;
private static var _mouseX:int;
private static var _mouseY:int;
public function Particle(xPos:int, yPos:int, sprite:Sprite):void {
_sp = sprite;
_sp.x = _originalX = xPos * 7;
_sp.y = _originalY = yPos * 7;
}
public static function setMousePosition(xPos:int, yPos:int):void {
_mouseX = xPos;
_mouseY = yPos;
}
public static function increaseMouseGravity():void {
_L *= 1.15;
}
public static function setMouseGravityToDefault():void {
_L = 100;
}
public function move():void {
_vx -= A0 * _vx + A1 * _x;
_vy -= A0 * _vy + A1 * _y;
_u = _mouseX - _sp.x;
_v = _mouseY - _sp.y;
_l = _u * _u + _v * _v + _L * 1.5;
_x += _vx + _u / _l * _L;
_y += _vy + _v / _l * _L;
_sp.x = _originalX + _x;
_sp.y = _originalY + _y;
}
}