風が吹くようななにか
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.ColorTransform;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;
import net.hires.debug.Stats;
[SWF(frameRate=60, width=465, height=465, backgroundColor=0x000000)]
public class Tmp extends Sprite
{
private var _object:Shape;
private var _canvas:BitmapData;
private var _rect:Rectangle;
private var _particles:Vector.<Particle>;
private var _mouseDowned:Boolean;
public function Tmp()
{
addEventListener(Event.ADDED_TO_STAGE, initialize);
}
private function initialize(evt:Event):void
{
removeEventListener(evt.type, arguments.callee);
_particles = new Vector.<Particle>();
_rect = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight);
_canvas = new BitmapData(stage.stageWidth, stage.stageHeight, true, 0);
addChild(new Bitmap(_canvas));
_object = new Shape();
with (_object.graphics)
{
beginFill(0xDFFE9E);
drawRect(-5, -2, 10, 4);
endFill();
}
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,
mtx:Matrix,
ctf:ColorTransform;
if (_mouseDowned)
{
for (i=0; i<5; i++)
{
addParticle();
}
}
_canvas.lock();
_canvas.fillRect(_canvas.rect, 0);
l = _particles.length;
for (i=0; i<l; i++)
{
p = _particles[i];
p.update();
mtx = new Matrix();
mtx.rotate(p.rotation);
mtx.translate(p.x, p.y);
ctf = new ColorTransform();
ctf.alphaMultiplier = p.alpha;
_canvas.draw(_object, mtx, ctf);
if (p.alpha <= 0 || !_rect.contains(p.x, p.y))
{
_particles.splice(i--, 1);
l = _particles.length;
}
}
_canvas.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;
}
}
}
class Particle
{
private var _x:Number;
public function get x():Number
{
return _x;
}
private var _y:Number;
public function get y():Number
{
return _y;
}
private var _rotation:Number;
public function get rotation():Number
{
return _rotation;
}
private var _alpha:Number;
public function get alpha():Number
{
return _alpha;
}
private var _vx:Number;
private var _vy:Number;
private var _tick:uint;
public function Particle(x:Number, y:Number)
{
var strength:Number,
angle:Number;
strength = Math.random() * 4;
angle = Math.random() * Math.PI * 2;
_x = x;
_y = y;
_vx = strength * Math.cos(angle);
_vy = strength * Math.sin(angle);
_rotation = Math.floor(Math.random() * 360);
_alpha = 1;
_tick = 0;
}
public function update():void
{
var t:Number;
_tick++;
t = _tick / 60;
_x += 9 * t + _vx;
_y += -3 * t + _vy;
_rotation += .3;
_alpha -= .02;
}
}