forked from: forked from: 風が吹くようななにか
Graphics 使って高速化してみたよ。
// forked from soundkitchen's forked from: 風が吹くようななにか
// forked from soundkitchen's 風が吹くようななにか
/**
* Graphics 使って高速化してみたよ。
*/
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.BlendMode;
import flash.display.Graphics;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.geom.Rectangle;
import net.hires.debug.Stats;
[SWF(frameRate=60, width=465, height=465, backgroundColor=0x000000)]
public class Main extends Sprite
{
private var _canvas:Shape;
private var _film:BitmapData;
private var _rect:Rectangle;
private var _particles:Vector.<Particle>;
private var _mouseDowned:Boolean;
public function Main()
{
addEventListener(Event.ADDED_TO_STAGE, initialize);
}
private function initialize(evt:Event):void
{
removeEventListener(evt.type, arguments.callee);
_particles = new Vector.<Particle>();
_canvas = new Shape();
_rect = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight);
_film = new BitmapData(stage.stageWidth, stage.stageHeight, true, 0);
addChild(new Bitmap(_film));
//addChild( _canvas );
addChildAt(new Stats(), 0);
addEventListener(Event.ENTER_FRAME, step);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
}
private function step(evt:Event):void
{
var i:int,
l:int,
p:Particle,
m:Point,
g:Graphics;
if (_mouseDowned)
{
for (i=0; i<5; i++)
{
addParticle();
}
}
g = _canvas.graphics;
g.clear();
l = _particles.length;
for (i=0; i<l; i++)
{
p = _particles[i];
p.update();
g.beginFill(0xDFFE9E, p.alpha);
for each (m in p.points)
{
if (m == p.points[0])
{
g.moveTo(p.x + m.x, p.y + m.y);
}
else
{
g.lineTo(p.x + m.x, p.y + m.y);
}
}
g.endFill();
if (p.alpha <= 0 || !_rect.contains(p.x, p.y))
{
_particles.splice(i--, 1);
l--;
}
}
_film.lock();
_film.fillRect(_film.rect, 0);
_film.draw(_canvas, null, null);
_film.unlock();
}
private function addParticle():void
{
_particles.push(new Particle(mouseX, mouseY));
}
private function mouseDownHandler(evt:MouseEvent):void
{
_mouseDowned = true;
}
private function mouseUpHandler(evt:MouseEvent):void
{
_mouseDowned = false;
}
}
}
import flash.geom.Point;
class Particle
{
// TODO: remove such a poor code.
public static const FRAME_RATE:int = 60;
public static const WIND:Number = 9;
public static const GRAVITY:Number = -3;
// declared each rect positions.
private static var P1:Point = new Point(-5, -2);
private static var P2:Point = new Point(5, -2);
private static var P3:Point = new Point(5, 2);
private static var P4:Point = new Point(-5, 2);
private static var DEFAULT_POINTS:Array = [P1, P2, P3, P4];
public var points:Vector.<Point>;
public var x:Number;
public var y:Number;
public var alpha:Number;
private var _vx:Number;
private var _vy:Number;
private var _tick:uint;
public function Particle(x:Number, y:Number)
{
this.x = x;
this.y = y;
this.alpha = 1;
this.points = new Vector.<Point>(4, true);
var strength:Number,
angle:Number,
i:int,
a:Number,
p:Point;
angle = Math.random() * Math.PI * 2;
for (i=0; i<DEFAULT_POINTS.length; i++)
{
p = DEFAULT_POINTS[i];
strength = Math.sqrt(p.x * p.x + p.y * p.y);
a = Math.atan2(p.y, p.x);
a += angle;
points[i] = Point.polar(strength, a);
}
strength = Math.random() * 4;
_vx = strength * Math.cos(angle);
_vy = strength * Math.sin(angle);
_tick = 0;
}
public function update():void
{
var t:Number,
p:Point,
strength:Number,
angle:Number;
_tick++;
t = _tick / FRAME_RATE;
x += WIND * t + _vx;
y += GRAVITY * t + _vy;
alpha -= .02;
for each (p in points)
{
strength = Math.sqrt(p.x * p.x + p.y * p.y);
angle = Math.atan2(p.y, p.x);
angle += Math.PI * 2 / FRAME_RATE;
p.x = strength * Math.cos(angle);
p.y = strength * Math.sin(angle);
}
}
}