forked from: 雪が降る
雪を降らせる。
必要になったので調べて作った。
// forked from sake's 雪が降る
/*
雪を降らせる。
必要になったので調べて作った。
*/
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.filters.BitmapFilter;
import flash.filters.BitmapFilterQuality;
import flash.filters.BlurFilter;
import flash.geom.ColorTransform;
import flash.geom.Matrix;
import flash.geom.Point;
import net.hires.debug.Stats;
[SWF(width="465", height="465", backgroundColor="0x000000", frameRate="40")]
public class Main extends Sprite
{
public static const BUGS_LENGTH:uint = 200;
private var _object:Shape;
private var _canvas:BitmapData;
private var _particles:Array;
private var _zero:Point;
private var _filter:BitmapFilter;
public function Main()
{
addEventListener(Event.ADDED_TO_STAGE, initialize);
}
private function initialize(evt:Event):void
{
removeEventListener(evt.type, arguments.callee);
var i:int,
p:Particle;
_particles = [];
_zero = new Point();
_filter = new BlurFilter();
_object = new Shape();
with (_object.graphics)
{
beginFill(0xFFFFFF);
drawCircle(0, 0, 3);
endFill();
}
_canvas = new BitmapData(stage.stageWidth, stage.stageHeight, true, 0);
addChild(new Bitmap(_canvas));
for(i=0; i<BUGS_LENGTH; i++)
{
_particles.push(new Particle(stage.stageWidth, stage.stageHeight));
}
addChild(new Stats());
addEventListener(Event.ENTER_FRAME, step);
}
private function step(evt:Event):void
{
var p:Particle,
mtx:Matrix,
ctf:ColorTransform;
_canvas.lock();
_canvas.fillRect(_canvas.rect, 0);
for each (p in _particles)
{
p.update();
// fix matrix settings.
mtx = new Matrix();
mtx.translate(p.x, p.y);
mtx.scale(p.scaleX, p.scaleY);
// fix color transform settings.
ctf = new ColorTransform();
ctf.alphaMultiplier = p.alpha;
_canvas.draw(_object, mtx, ctf);
}
_canvas.applyFilter(_canvas, _canvas.rect, _zero, _filter);
_canvas.unlock();
}
}
}
class Particle
{
private var _x:Number;
private var _y:Number;
private var _scaleX:Number;
private var _scaleY:Number;
private var _alpha:Number;
private var _tick:int;
private var _stageWidth:Number;
private var _stageHeight:Number;
public function get x():Number
{
return _x;
}
public function get y():Number
{
return _y;
}
public function get scaleX():Number
{
return _scaleX;
}
public function get scaleY():Number
{
return _scaleY;
}
public function get alpha():Number
{
return _alpha;
}
public function Particle(stageWidth:Number, stageHeight:Number)
{
_stageWidth = stageWidth;
_stageHeight = stageHeight;
_x = Math.random() * _stageWidth;
_y = Math.random() * _stageHeight;
_tick = Math.floor(Math.random() * 360);
var z:Number = Math.random() + .2;
_alpha = z;
_scaleX = z;
_scaleY = z;
}
public function update():void
{
var angle:Number = (_tick / 180) * Math.PI;
_x += Math.cos(angle);
_y += 1;
if (_y > _stageHeight)
{
_y = -5;
}
_tick = ++_tick % 360;
}
}