2D紙ふぶき forked from: bouncing balls in 3d space(3)
紙ふぶきっぽくしてみるw
/**
* Copyright harupiyo ( http://wonderfl.net/user/harupiyo )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/wf56
*/
// forked from harupiyo's forked from: forked from: bouncing balls in 3d space(3)
// forked from uwi's forked from: bouncing balls in 3d space(3)
// forked from k__'s bouncing balls in 3d space(3)
// forked from k__'s bouncing balls in 3d space
// 紙ふぶきっぽくしてみるw
package {
import flash.utils.IDataInput;
import flash.accessibility.Accessibility;
import flash.display.*;
import flash.events.*;
import flash.filters.*;
import flash.geom.*;
import net.hires.debug.Stats;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
[SWF(frameRate=60)]
public class Main extends Sprite {
private var particles:Array = [];
private var canvas:BitmapData;
public function Main() {
canvas = new BitmapData(stage.stageWidth,stage.stageHeight,false, 0x00000000 );
addChild( new Bitmap(canvas) );
addEventListener( Event.ENTER_FRAME, h_enterFrame );
addChild(new Stats());
createView();
}
private function h_enterFrame(evt:Event):void {
if (Math.random() < 0.5) {
particles.push( new Particle2D() );
particles.push( new Particle2D() ); // twice
}
draw();
}
private var bmd:BitmapData = new BitmapData(stage.stageWidth,stage.stageHeight,true,0x000000);
private function draw():void {
var i:int;
var p2d:Object;
bmd.fillRect( bmd.rect, 0xFF000000 );
for (i = particles.length - 1; i >= 0; i --) {
particles[i].update();
if (particles[i].life == 0) {
if(i == particles.length - 1){
particles.pop();
}else{
particles[i] = particles.pop();
}
}
}
for each(p2d in particles) {
bmd.fillRect( new Rectangle( p2d.x, p2d.y, 5, 5 ), p2d.color );
}
canvas.lock();
canvas.draw(bmd,null , null, BlendMode.NORMAL );
canvas.unlock();
}
//なるほど、こうやってtrace の文字を出力しているのか
private function createView():void {
var t:TextField = new TextField();
t.autoSize = TextFieldAutoSize.LEFT;
t.textColor = 0xFFFFFFFF;
addChild(t);
trace = function(s:String):void { t.appendText(s + "\n"); };
}
private var trace:Function;
}
}
class Particle2D {
import flash.display.*;
public var x:Number=0,y:Number=0;
public var vx:Number=0,vy:Number=0;
private var SCREEN_X_MAX:int = 550;
private var SCREEN_Y_MAX:int = 450;
public var life:int;
public var color:uint;
public var originalColor:uint;
public var isReverse:Boolean;
private var colors : Array = [
0xFFFF9300,
0xFF6DE20E,
0xFFB011FD,
0xFFF50107,
0xFF0992F7
];
private function _getRandomColor():uint {
return colors[ int(Math.random() * colors.length ) ];
}
function Particle2D() {
x = Math.random() * SCREEN_X_MAX;
y = 0;
vy = Math.random()*4 + 1; // 落下速度
life = 1;
color = originalColor = _getRandomColor();
isReverse = false;
}
public function update():void {
// ゆらゆらゆれながら落下
vx = Math.random()*5-2.5; // 左右にランダム移動
if ( y >= SCREEN_Y_MAX ) {
life=0;
}
if ( x <= 0 && SCREEN_X_MAX < x ) {
life=0;
}
// たまに裏返る
if( Math.random() > 0.9 ){
_reverse();
}
x+=vx;
y+=vy;
}
private function _reverse():void {
if( isReverse ) {
isReverse = false;
color = originalColor;
}
else{
isReverse = true;
color = 0xFFFFFFFF; // 色紙なので裏面は白
}
}
}