borowed particles
/**
* Copyright celsyum ( http://wonderfl.net/user/celsyum )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/u6j1
*/
// forked from paq's forked from: 重力マウス(ちょっぴり軽量化してみた)
// forked from fumix's 重力マウス(リンクリストにしてみた)
// forked from undo's 重力マウス
// リンクリストにしてみたけどそんなに速くない??
package
{
import flash.events.MouseEvent;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.geom.ColorTransform;
import flash.geom.Point;
import flash.geom.Rectangle;
import net.hires.debug.Stats;
[SWF(frameRate='60', width='465', height='465', backgroundColor='0xFFFFFF')]
public class ParticleTest1 extends Sprite
{
private var _bmp:Bitmap;
private var _bmd:BitmapData;
private var _bmdRect:Rectangle;
private var _colorTransform:ColorTransform = new ColorTransform(1, 1, 1, 0.9);
private var _first:Node;
private var _maxNum:int = 1000;
private var _rect:Rectangle = new Rectangle(0, 0, 1, 1);
private var gravPoint_x:Number = 0;//(gravPoint_x + Math.random()*200)%465;//mouseX;
private var gravPoint_y:Number = 0;
private var _rotation:Number = 0;
public function ParticleTest1()
{
this.stage.align = StageAlign.TOP_LEFT;
this.stage.scaleMode = StageScaleMode.NO_SCALE;
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(evt:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
var sp:Sprite = new Sprite();
sp.graphics.beginFill(0xDDEDFF);
sp.graphics.drawRect(80, 150, 90, 110);
sp.buttonMode = true;
sp.useHandCursor = true;
sp.addEventListener(MouseEvent.CLICK, doit);
addChild(sp);
var old:Node;
_bmd = new BitmapData(465, 465, true, 0x00FFFFFF);
_bmp = new Bitmap(_bmd);
addChild(_bmp);
this._bmdRect = new Rectangle(0, 0, 465, 465);
for (var i:int = 0; i < this._maxNum; i++)
{
var n:Node = new Node();
n.pos_x = 230;//Math.random() * 465;
n.pos_y = 430;//Math.random() * 465;
n.grav_x = n.pos_x - 10 + Math.random()*20;
n.grav_y = n.pos_y - 10 + Math.random()*20;
if (_first == null) {
old = _first = n;
} else {
old.next = n;
old = n;
}
}
addChild(new Stats());
addEventListener(Event.ENTER_FRAME, onEnter);
}
private function doit(evt:MouseEvent):void
{
evt.currentTarget.x += 4;
}
private function onEnter(evt:Event):void
{
//var gravPoint:Point = new Point(mouseX, mouseY);
_rotation += 0.025;
gravPoint_x = 200;//Math.sin(_rotation) * 200 + 230 ;//(gravPoint_x + Math.random()*30)%465;//mouseX;
gravPoint_y = 250;//Math.cos(_rotation) * 200 + 230; //(gravPoint_y + Math.random()*30)%465;//mouseY;
var n:Node = _first;
this._bmd.lock();
do
{
//var n:Node = this._nodeArray[i] as Node;
//var diff:Point = new Point(gravPoint.x - n.pos.x, gravPoint.y - n.pos.y);
//var diff_x:Number = gravPoint_x - n.pos_x;
//var diff_y:Number = gravPoint_y - n.pos_y;
var diff_x:Number = n.grav_x - n.pos_x;
var diff_y:Number = n.grav_y - n.pos_y;
var rad:Number = Math.atan2(diff_y, diff_x);
var grav:Number = Math.sqrt(diff_x * diff_x + diff_y * diff_y);
//n.grav_y += 5;
n.acc_x = (Math.cos(rad) * (20/grav)%20);
n.acc_y = (Math.sin(rad) * (20/grav)%20);
n.v_x += n.acc_x;
n.v_y += n.acc_y;
n.pos_x += n.v_x;
n.pos_y += n.v_y;
n.acc_x *= 0.98;
n.acc_y *= 0.98;
n.v_x *= 0.96;
n.v_y *= 0.96;
if (n.pos_x > 465)
n.pos_x = 0;
else if (n.pos_x < 0)
n.pos_x = 465;
if (n.pos_y > 465)
n.pos_y = 0;
else if (n.pos_y < 0)
n.pos_y = 465;
if (grav < 5) //if (n.life < 0)
{
n.life = Math.random()*80+135;
n.pos_x = gravPoint_x;
n.pos_y = gravPoint_y;
n.grav_x = Math.sin(_rotation) * 200 + 230 ;//n.pos_x - 300 + Math.random()*600;
n.grav_y = Math.cos(_rotation) * 200 + 230 ;//n.pos_y - 300+ Math.random()*600;
}
n.life -= 1;
_rect.x = n.pos_x;
_rect.y = n.pos_y;
n.grav_x = Math.sin(_rotation) * 100 + 230
n.grav_y = Math.cos(_rotation) * 100 + 230
//this._bmd.fillRect(_rect, 0xFFFFFF);
this._bmd.setPixel32(n.pos_x, n.pos_y, 0xFF000000);
}
while (n = n.next);
this._bmd.colorTransform(this._bmdRect, this._colorTransform);
this._bmd.unlock();
}
}
}
import flash.geom.Point;
class Node
{
public var acc_x:Number;
public var acc_y:Number;
public var v_x:Number = 0;
public var v_y:Number = 0;
public var pos_x:Number = 0;
public var pos_y:Number = 0;
public var grav_x:Number = 230;
public var grav_y:Number = 430;
public var life:Number = 10;
public var next:Node;
}