bit.fall in silico
bit.fall in silico
julius popp というアーチストの作品 "bit.fall" を ActionScript 3.0 でそれっぽくコーディング
参照:http://www.youtube.com/watch?v=ygQHj1W0PPM
パーティクル周りのコーディングは Saqoosha さんの "snow(Dictionary 版)" がベース
http://wonderfl.kayac.com/code/e07aa63c21f14e579ec1c929340c4f4de520be28
// bit.fall in silico
// julius popp というアーチストの作品 "bit.fall" を ActionScript 3.0 でそれっぽくコーディング
// 参照:http://www.youtube.com/watch?v=ygQHj1W0PPM
// パーティクル周りのコーディングは Saqoosha さんの "snow(Dictionary 版)" がベース
// http://wonderfl.kayac.com/code/e07aa63c21f14e579ec1c929340c4f4de520be28
package {
/**
* @author YOSHIDA, Akio
*/
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.utils.Timer;
import flash.utils.Dictionary;
import flash.system.Security;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFieldAutoSize;
[SWF(width = 465, height = 465, backgroundColor = 0x000000, frameRate = 120)]
public class Main extends Sprite {
Wonderfl.capture_delay(10);
private static const GRAVITY:Number = 9.8;
private static const DERAY:Number = 8.39; // (1 / frameRate) * 1000 を基準に;
private var urlLoader:URLLoader;
private var canvas:BitmapData;
private var data:Array;
private var aryDisplayData:Array;
private var dicParticle:Dictionary = new Dictionary();
private var timer:Timer;
private var current:uint = 0;
private var sign:TextField; // サイン表示
public function Main() {
Security.loadPolicyFile("http://5ivestar.org/proxy/crossdomain.xml");
urlLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, loadCompletehandler);
urlLoader.load(new URLRequest("http://5ivestar.org/proxy/http://homepage3.nifty.com/aquioux/swf/as3/wonderfl/bitfalldata.txt"));
// ナウローディング
sign = new TextField();
sign.defaultTextFormat = new TextFormat("_sans", 16, 0xffffff);
sign.text = "Now Loading...";
sign.autoSize = TextFieldAutoSize.LEFT
sign.x = 20;
sign.y = stage.stageHeight - 38;
addChild(sign);
}
private function loadCompletehandler(e:Event):void {
var str:String = crlf2lf(urlLoader.data);
data = dataExpand(str); // データ展開
canvas = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0x000000);
addChildAt(new Bitmap(canvas),0);
timer = new Timer(DERAY);
stage.addEventListener(MouseEvent.CLICK, clickHandler);
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
sign.text = "Click stage";
}
private function crlf2lf(str:String):String {
// 「CR [\r] (Mac)」 「LF [\n] (Unix)」 「CR+LF [\r\n] (Dos)」
str = str.replace(/\r\n/g, "\n");
str = str.replace(/\r/g, "\n");
return str;
}
private function clickHandler(e:MouseEvent):void {
if (sign != null){
removeChild(sign)
sign = null;
}
urlLoader = null;
createDisplayData(); // 表示データ配列を整える
stage.removeEventListener(MouseEvent.CLICK, clickHandler);
timer.reset();
timer.repeatCount = aryDisplayData.length;
timer.addEventListener(TimerEvent.TIMER, timerHandler);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerCompleteHandler);
timer.start();
}
private function timerHandler(e:TimerEvent):void {
emit(); // パーティクル発生
}
private function timerCompleteHandler(e:TimerEvent):void {
stage.addEventListener(MouseEvent.CLICK, clickHandler);
timer.removeEventListener(TimerEvent.TIMER, timerHandler);
timer.removeEventListener(TimerEvent.TIMER_COMPLETE, timerCompleteHandler);
}
private function enterFrameHandler(e:Event):void {
render(); // パーティクル描画
}
// データ展開
private function dataExpand(str:String):Array {
var arySource:Array = str.split("\n\n");
var aryData:Array = [];
for each (var element:String in arySource) {
var arySource1:Array = element.split("\n");
var aryData1:Array = [];
for each (var element1:String in arySource1) {
aryData1.push(element1.split(","));
}
aryData1.reverse();
aryData.push(aryData1);
}
return aryData;
}
// 表示データ配列を整える
private function createDisplayData():void {
aryDisplayData = data[current++];
current %= data.length;
}
// パーティクル発生
private function emit():void {
var margin:uint = 20;
var ary:Array = aryDisplayData[timer.currentCount - 1];
for each (var element:uint in ary) {
dicParticle[new Particle(element + margin, margin)] = true;
}
}
// パーティクル描画
private function render():void {
canvas.lock();
canvas.fillRect(canvas.rect, 0x000000);
var gravity:Number = GRAVITY / 750;
for (var key:Object in dicParticle) {
var p:Particle = Particle(key);
p.update(gravity);
var flg:Boolean = p.isLimitOver(stage.stageHeight);
if (flg) {
delete dicParticle[p];
} else {
p.render(canvas);
}
}
canvas.unlock();
}
}
}
import flash.display.BitmapData;
class Particle {
private var x:Number;
private var y:Number;
private var vy:Number;
private var color:uint;
public function Particle(x:Number = 0, y:Number = 0, color:uint = 0xffffff) {
vy = 0.4;
this.x = x;
this.y = y;
this.color = color;
}
// アップデート(このクラスのインスタンスを呼び出すクラスの ENTER_FRAME で)
public function update(gravity:Number):void {
vy += (gravity * (Math.random() + 1));
y += vy;
}
// ステージ領域内外の判定
public function isLimitOver(limit:Number):Boolean {
return (y > limit) ? true : false;
}
// 描画
public function render(canvas:BitmapData):void {
canvas.setPixel(x, y, color);
}
}