TestTwinkle
TestTwinkle
/**
* Copyright ProjectNya ( http://wonderfl.net/user/ProjectNya )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/kf1x
*/
////////////////////////////////////////////////////////////////////////////////
// TestTwinkle
////////////////////////////////////////////////////////////////////////////////
package {
import flash.display.Sprite;
import flash.geom.Rectangle;
[SWF(backgroundColor="#333333", width="465", height="465", frameRate="60")]
public class Main extends Sprite {
private var twinkle:TwinkleEffect;
public function Main() {
//Wonderfl.capture_delay(1);
init();
}
private function init():void {
graphics.beginFill(0x333333);
graphics.drawRect(0, 0, 465, 465);
graphics.endFill();
//
var rect:Rectangle = new Rectangle(0, 0, 465, 465);
var area:Rectangle = new Rectangle(182, 182, 100, 100);
twinkle = new TwinkleEffect(rect, area);
addChild(twinkle);
twinkle.setup(100, 10);
twinkle.start();
}
}
}
//////////////////////////////////////////////////
// TwinkleEffectクラス
//////////////////////////////////////////////////
import flash.display.Sprite;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.geom.Rectangle;
import flash.geom.Matrix;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
class TwinkleEffect extends Sprite {
private var rect:Rectangle;
private var area:Rectangle;
private var bitmapData:BitmapData;
private var bitmap:Bitmap;
private static var radius:uint = 10;
private var interval:uint = 100;
private var life:uint = 10;
private var particles:Array;
private var unit:uint = 30;
private var division:uint = 12;
private static var radian:Number = Math.PI/180;
private var timer:Timer;
public function TwinkleEffect(r:Rectangle, a:Rectangle) {
rect = r;
area = a;
init();
}
private function init():void {
bitmapData = new BitmapData(rect.width, rect.height, true, 0x00000000);
bitmap = new Bitmap(bitmapData);
addChild(bitmap);
particles = new Array();
}
public function setup(i:uint = 100, l:uint = 10):void {
interval = i;
life = l;
}
public function start():void {
timer = new Timer(interval);
timer.addEventListener(TimerEvent.TIMER, create, false, 0, true);
timer.start();
addEventListener(Event.ENTER_FRAME, update, false, 0, true);
}
public function stop():void {
timer.stop();
removeEventListener(Event.ENTER_FRAME, update);
}
private function create(evt:TimerEvent):void {
var px:Number = area.x + Math.random()*area.width;
var py:Number = area.y + Math.random()*area.height;
plot(px, py);
}
private function plot(px:Number, py:Number):void {
var matrix:Matrix = new Matrix();
matrix.translate(px - radius, py - radius);
var particle:TwinkleParticle = new TwinkleParticle();
particle.x = px;
particle.y = py;
particle.life = life;
particle.matrix = matrix;
particle.color = 0xFFFFFF;
particles.push(particle);
bitmapData.lock();
draw(px, py, 0xFFFFFF, life/life);
bitmapData.unlock();
}
private function update(evt:Event):void {
bitmapData.lock();
bitmapData.fillRect(rect, 0x00000000);
for (var n:uint = 0; n < particles.length; n++) {
var particle:TwinkleParticle = particles[n];
draw(particle.x, particle.y, particle.color, particle.life/life);
particle.life --;
if (particle.life < 0) {
particles.splice(n, 1);
particle = null;
}
}
bitmapData.unlock();
}
private function draw(px:uint, py:uint, color:uint, percent:Number):void {
var length:uint = uint(radius*percent);
var argb:uint = 0xFF << 24 | color;
for (var n:uint = 0; n < length; n++) {
for (var a:uint = 0; a < 360; a+=unit) {
var dx:Number = px + n*Math.cos(a*radian);
var dy:Number = py + n*Math.sin(a*radian);
bitmapData.setPixel32(dx, dy, argb);
}
}
}
}
//////////////////////////////////////////////////
// TwinkleParticleクラス
//////////////////////////////////////////////////
import flash.geom.Matrix;
class TwinkleParticle {
public var x:uint;
public var y:uint;
public var life:int;
public var matrix:Matrix;
public var color:uint;
public function TwinkleParticle() {
}
}