Particle part2
パーティクルが必要になったのでざっくり作ってみた。
まだ途中。
ドラッグ中にぱーってっくるー
part2
回転とかアルファとか大きさとか
眠いのでいろいろてっけとー
ちょっと重くなった
こちらを参考に作らせていただきました。
パーティクルクラスを作ってみた |5ive™.blog
http://www.5ive.info/blog/archives/78
// forked from ll_koba_ll's Particle part1
/*
パーティクルが必要になったのでざっくり作ってみた。
まだ途中。
ドラッグ中にぱーってっくるー
part2
回転とかアルファとか大きさとか
眠いのでいろいろてっけとー
ちょっと重くなった
こちらを参考に作らせていただきました。
パーティクルクラスを作ってみた |5ive™.blog
http://www.5ive.info/blog/archives/78
*/
package
{
import flash.display.*;
import flash.events.*;
import flash.system.*;
import net.hires.debug.Stats;
[SWF(frameRate="30", backgroundColor="#000000")]
public class ParticlesTest extends Sprite
{
private var pr:ParticleRender;
private var mouseDown:Boolean = false;
public function ParticlesTest()
{
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
stage.align = StageAlign.TOP_LEFT;
stage.quality = StageQuality.HIGH;
stage.scaleMode = StageScaleMode.NO_SCALE;
var sp:Sprite = new Sprite();
with(sp.graphics)
{
beginFill(0xFFFFFF);
//drawCircle(0,0,10);
drawRect(0, 0, 20, 10);
endFill();
}
// ここでパーティクルの設定もろもろ
pr = new ParticleRender(500, 500, 5, 4, 5, sp);
pr.gravity = -1;
pr.wind = 0;
pr.vrotation = 10;
pr.valpha = -0.03;
pr.vscale = -0.01;
addChild(pr);
stage.addEventListener(MouseEvent.MOUSE_DOWN, down);
stage.addEventListener(MouseEvent.MOUSE_UP, up);
addEventListener(Event.ENTER_FRAME, enterFrame);
addChild(new Stats());
}
private function down(e:MouseEvent):void
{
mouseDown = true;
}
private function up(e:MouseEvent):void
{
mouseDown = false;
}
private function enterFrame(e:Event):void
{
if (mouseDown) pr.start(mouseX, mouseY);
}
}
}
import flash.display.*;
import flash.utils.Timer;
import flash.events.*;
import flash.geom.*;
class ParticleRender extends Sprite
{
private var _canvas:BitmapData;
private var _particleNum:int
private var _particles:Array;
private var _view:DisplayObjectContainer;
private var _vx:Number;
private var _vy:Number;
private var _gravity:Number;
public function get gravity():Number { return _gravity; }
public function set gravity(value:Number):void { _gravity = value; }
private var _wind:Number;
public function get wind():Number { return _wind; }
public function set wind(value:Number):void { _wind = value; }
private var _vrotation:Number;
public function get vrotation():Number { return _vrotation; }
public function set vrotation(value:Number):void { _vrotation = value; }
private var _valpha:Number
public function get valpha():Number { return _valpha; }
public function set valpha(value:Number):void { _valpha = value; }
private var _vscale:Number;
public function get vscale():Number { return _vscale; }
public function set vscale(value:Number):void { _vscale = value; }
private var _lifeTime:Number;
/**
* @param w canvas width
* @param h canvas height
* @param vx max speed x
* @param vy max speed y
* @param particleNum paritcle number
* @param view particle view
* @param gravity
* @param wind
* @param vrotation vx vyと同じでmaxとしてランダム+-
* @param valpha renderで加算
* @param vscale renderで加算
*/
public function ParticleRender(w:Number,
h:Number,
vx:Number,
vy:Number,
particleNum:int,
view:DisplayObjectContainer,
gravity:Number = 0,
wind:Number = 0,
vrotation:Number = 0,
valpha:Number = 0,
vscale:Number = 0
):void
{
_canvas = new BitmapData(w,h,true,0x00FFFFFF);
addChild(new Bitmap(_canvas));
_particleNum = particleNum;
_gravity = gravity;
_wind = wind;
_particles = [];
_view = view;
_vx = vx;
_vy = vy;
_vrotation = rotation;
_valpha = 0;
_vscale = 0;
addEventListener(Event.ENTER_FRAME, update);
}
public function start(x:Number = 0, y:Number = 0):void
{
var tmp:Particle;
for (var i:int =0; i<_particleNum; i++)
{
tmp = new Particle();
tmp.vx = randomRange(-_vx, _vx);
tmp.vy = randomRange(-_vy, _vy);
tmp.x = x;
tmp.y = y;
tmp.rotation = randomRange(-_vrotation, _vrotation);
_particles.push(tmp);
}
}
public function stop():void
{
}
private function update(e:Event):void
{
_canvas.lock();
_canvas.fillRect(this._canvas.rect, 0x00FFFFFF);
var mtx:Matrix;
var ct:ColorTransform;
var p:Particle;
var l:int = _particles.length;
for (var i:int = 0; i<l; i++)
{
p = _particles[i];
p.update();
p.vy += _gravity*0.1;
p.vx += _wind*0.1;
p.scale = (p.scale <= 0)? 0:p.scale+_vscale;
p.alpha += _valpha;
//_view.alpha = p.alpha;
mtx = new Matrix();
mtx.scale(p.scale, p.scale);
mtx.rotate(p.rotation);
mtx.translate(p.x, p.y);
ct = new ColorTransform();
ct.alphaMultiplier = p.alpha;
_canvas.draw(_view, mtx, ct);
if (p.x < 0 || p.x > _canvas.width ||
p.y < 0 || p.y > _canvas.height)
{
_particles[i] = null;
_particles.splice(i, 1);
l = _particles.length;
i--;
}
}
_canvas.unlock();
}
private function randomRange(min:Number, max:Number):Number
{
return (Math.random() * (max - min)) + min;
}
}
class Particle
{
public var x:Number;
public var y:Number;
public var vx:Number;
public var vy:Number;
public var rotation:Number;
public var alpha:Number;
public var scale:Number;
public var lifetime:Number;
public function Particle()
{
alpha = 1;
scale = 1;
}
public function update():void
{
x += vx;
y += vy;
}
}