Forkしつつ花火の作り方学んでみた
/**
* Copyright 7kamura ( http://wonderfl.net/user/7kamura )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/vdTm
*/
package {
import flash.display.*;
import flash.events.*
import flash.geom.*;
import flash.geom.ColorTransform;
[SWF(width="465", height="465", backgroundColor="0x000000", frameRate="30")];
import flash.utils.Timer;
import flash.filters.BlurFilter;
public class Main extends Sprite {
public const WIDTH:Number = 465;
public const HEIGHT:Number = 465;
private var _rect:Rectangle;
private var _particles:Array;
private var _canvas:BitmapData;
private var _glow:BitmapData;
private var _cTra:ColorTransform;
private var _timer:Timer;
private var _sx:Number;
private var _sy:Number;
public function Main():void {
init();
}
private function init():void {
_particles = [];
_canvas = new BitmapData(WIDTH, HEIGHT, false, 0x0);
addChild(new Bitmap(_canvas)) as Bitmap;
_glow = new BitmapData(WIDTH/4, HEIGHT/4, false, 0x0);
var bmp:Bitmap = addChild(new Bitmap(_glow, PixelSnapping.NEVER, true)) as Bitmap;
bmp.scaleX = bmp.scaleY = 4;
bmp.blendMode = BlendMode.ADD;
_rect = new Rectangle(0, 0, WIDTH, HEIGHT);
_cTra = new ColorTransform(.8, .8, .9, 1.0);
stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
_timer = new Timer(400);
_timer.addEventListener(TimerEvent.TIMER, onTimer);
_timer.start();
}
private function enterFrameHandler(e:Event):void {
_canvas.lock();
var i:int = _particles.length;
while(i--) {
var p:Particle = _particles[i];
p.vy += 0.2; // 重力加速度
p.vx *= 0.9; // 摩擦
p.vy *= 0.9; // 摩擦
p.x += p.vx; // 移動する
p.y += p.vy;
_canvas.setPixel32(p.x, p.y, p.color);
if( p.x > stage.stageWidth || p.x < 0 || p.y > stage.stageHeight || p.y < 0 || Math.abs(p.vx) < .01 || Math.abs(p.vy) < .01)
_particles.splice(i, 1);
}
_canvas.applyFilter(_canvas, _rect, new Point(), new BlurFilter(1,1));
_canvas.colorTransform(_rect, _cTra);
_canvas.unlock();
_glow.draw(_canvas, new Matrix(0.25, 0, 0, 0.25));
}
private function onTimer(e:TimerEvent):void {
(_cTra.redMultiplier > 0.9)? _cTra.redMultiplier = 0.8 : _cTra.redMultiplier += .01;
createHanabi();
}
private function createHanabi():void {
_sx = Math.random() * WIDTH;
_sy = Math.random() * HEIGHT /3;
var i:int = 200;
while(i--)
createParticle();
}
private function createParticle():void {
var p:Particle = new Particle();
p.x = _sx;
p.y = _sy;
var angle:Number = Math.random() * Math.PI * 2;
var radius:Number = Math.sqrt(Math.random()) * 10; // 5.0以上の出現確率が75%
p.vx = Math.cos(angle) * radius;
p.vy = Math.sin(angle) * radius;
_particles.push(p);
}
}
}
class Particle {
public var x:Number;
public var y:Number;
public var vx:Number;
public var vy:Number;
public var color:uint;
public function Particle():void {
x = 0;
y = 0;
vx = 0;
vy = 0;
color = 0xFFFFFFFF;
}
}