forked from: forked from: forked from: tree
無駄に幻想的な感じをだしてみた。草っぽくなった?
// forked from izm_design's forked from: forked from: tree
// forked from egyu2's forked from: tree
// 無駄に幻想的な感じをだしてみた。草っぽくなった?
// forked from egyu2's tree
// write as3 code here..
package
{
import flash.display.StageScaleMode;
import flash.display.StageAlign;
import flash.display.Sprite;
import flash.events.Event;
import flash.display.Bitmap;
import flash.display.BitmapData;
public class Main extends Sprite
{
public var b:Bitmap;
public var bd:BitmapData;
public var effect:Effect;
[SWF(width="465", height="465", backgroundColor="0x000000", frameRate="10")]
public function Main()
{
var mainSp:Sprite = new Sprite();
this.addChild(mainSp);
//mainSp.scaleX = mainSp.scaleY = 0.5;
this.stage.scaleMode = StageScaleMode.NO_SCALE;
this.stage.align = StageAlign.TOP_LEFT;
//this.bd = new BitmapData(this.stage.stageWidth * 2, this.stage.stageHeight * 2);
this.bd = new BitmapData(this.stage.stageWidth, this.stage.stageHeight);
this.b = new Bitmap(this.bd);
this.b.smoothing = true;
mainSp.addChild(this.b);
this.effect = new Effect(this.b, this.bd);
this.effect.alpha = 0.8;
this.addEventListener(Event.ENTER_FRAME , onEnterFrame);
addChild(this.effect);
var l2:Line = new Line(this.b,this.bd);
l2.init();
l2.positionX = mainSp.stage.stageWidth / 2;
l2.positionY = mainSp.stage.stageHeight;
l2.addAccele(0 , -3);
mainSp.addChild(l2);
}
private function onEnterFrame(e:Event):void
{
this.effect.doEffect(this.bd);
}
}
}
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Graphics;
import flash.display.Sprite;
import flash.events.Event;
class Line extends Sprite
{
public var positionX:Number = 0;
public var positionY:Number = 0;
public var positionX_old:Number = 0;
public var positionY_old:Number = 0;
public var speedX:Number = 0;
public var speedY:Number = 0;
public var acceleX:Number = 0;
public var acceleY:Number = 0;
public var friction:Number = 0.99;
public var life:Number = 2;
public var color:Number = 0;
public var colorToBlack:Boolean = false;
public var b:Bitmap;
public var bd:BitmapData;
private var isChild:Boolean = false;
public function Line(b:Bitmap,bd:BitmapData)
{
this.b = b;
this.bd = bd;
}
public function init():void
{
this.addEventListener(Event.ENTER_FRAME , onEnterFrame);
}
public function addAccele(x:Number , y:Number):void
{
this.acceleX = x;
this.acceleY = y;
}
private function onEnterFrame(e:Event):void
{
//スピード計算
this.calcSpeed();
//たまに加速度を追加
this.randomAddAccele();
//新しいLineを作る
if (
Math.random() > 0.96
&&
( Math.abs(this.speedX) + Math.abs(this.speedY) ) > 0.5 //一定のスピードがあるものだけ
) this.birth();
//描画
this.draw();
//スピードが遅くなったら
if ( ( Math.abs(this.speedX) + Math.abs(this.speedY) ) < 0.1 )
{
this.removeEventListener(Event.ENTER_FRAME , onEnterFrame);
}
if (!this.colorToBlack) this.color += 3;
if (this.colorToBlack) this.color -= 3;
if (this.color >= 200) this.colorToBlack = true;
if (this.color <= 0) this.colorToBlack = false;
}
/**
* 新しいLineを作成する
*/
private function birth():void
{
var newChild:Line = this.getCopySelf();
newChild.life--;
if (newChild.life >= 0)
{
newChild.isChild = true;
//newChild.acceleX = this.speedX * Math.random() * 0.2;
//newChild.acceleY = this.speedY * Math.random() * 0.2;
this.addChild(newChild);
newChild.init();
}
else
{
newChild = null;
}
}
/**
* 自分自身をコピーしたものを返す
* @return Line
*/
private function getCopySelf():Line
{
var newChild:Line = new Line(this.b,this.bd);
newChild.positionX = this.positionX;
newChild.positionY = this.positionY;
newChild.speedX = this.speedX;
newChild.speedY = this.speedY;
newChild.acceleX = this.acceleX;
newChild.acceleY = this.acceleY;
newChild.friction = this.friction;
newChild.life = this.life;
newChild.color = this.color;
return newChild;
}
/**
* たまに加速度を追加する。
*/
private function randomAddAccele():void
{
var addX:Number = 0;
var addY:Number = 0;
var power:Number = Math.abs(this.speedX) + Math.abs(this.speedY);
addX = Math.random() * (power * 0.2);
addY = Math.random() * (power * 0.2);
addX = addX - Math.random() * 2 * addX;
addY = addY - Math.random() * 2 * addY;
if (Math.random() > 0.5) addX = 0;
if (Math.random() > 0.5) addY = 0;
this.speedX += addX;
this.speedY += addY;
}
/**
* スピード・加速度・抵抗などを計算する。
*/
private function calcSpeed():void
{
//加速度加算
this.speedX += this.acceleX;
this.speedY += this.acceleY;
//加速度を0に戻す
this.acceleX = 0;
this.acceleY = 0;
//抵抗を乗算
this.speedX *= this.friction;
this.speedY *= this.friction;
//移動する前の位置を保持
this.positionX_old = this.positionX;
this.positionY_old = this.positionY;
//移動
this.positionX += this.speedX;
this.positionY += this.speedY;
}
/**
* 描画する
*/
private function draw():void
{
var g:Graphics = this.graphics;
var thick:Number = 1;
var color:uint = int(this.color) << 16 | int(this.color) << 8 | int(this.color);
thick = (Math.abs(this.speedX) + Math.abs(this.speedY) ) * 5;//速さによって
if (thick > 10) thick = 10;
//thick = this.life * 3;//寿命によって
g.clear();
g.moveTo(this.positionX_old , this.positionY_old);
g.lineStyle(thick , color , 1);
g.lineTo(this.positionX , this.positionY);
this.bd.draw(this);
}
}
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.filters.BlurFilter;
class Effect extends Bitmap
{
private var b:Bitmap;
private var bd:BitmapData;
public function Effect(b:Bitmap , bd:BitmapData)
{
this.b = b;
this.bd = bd;
var thisBd:BitmapData = new BitmapData(this.b.width, this.b.height, true, 0x00009900);
this.bitmapData = thisBd;
}
public function doEffect(bd:BitmapData):void
{
this.bitmapData = bd.clone();
var f:BlurFilter = new BlurFilter(50, 50, 2);
this.filters = [f];
}
}