package { /** * 実験用のフレームワーク&テンプレート * 毎フレームなんかする系用 * * ・フレームワーク * ・初期化 * ・処理中 * ・リセット * ・UI * ・画面をクリックして開始停止 * ・リセット * ・プリセットクラス * ・四角 (Rectクラス) * ・円 (Ballクラス) * ・四角ボタン (RectButtonクラス) * ・画像ローダー (ImageLoaderクラス) * ・パーティクル (Particleクラス) * ・プリセット定数 * ・ゼロポイント * * (実装予定・クリックで処理を切り替える仕組み) * (実装予定・他にも簡単なグラフィック用のクラスを用意) * * @author doke@kayac.com * last modified 2009/3/14 * */ public class Template extends Sprite { // パラメータ private var _bmpBase:BitmapData; private var _bmp:DisplayObject; private const O:Point = new Point(); private var color:ColorTransform = new ColorTransform(0.9, 0.9, 0.9, 1, 0, 0, 0, 0 ); // 初期化処理 private var _particles:Array = []; private function setupStage( W:Number, H:Number ) :void { for( var i :uint =0; i< 200; ++i ) { var ball:DisplayObject = addChild( new Ball( Math.random()*10-5 ) ); ball.x = stage.stageWidth * 0.5; ball.y = stage.stageHeight * 0.5; _particles.push( new Particle( ball, 60, Math.random()*100-50, Math.random()*100-50 ) ); } } // EnterFrameで呼ばれる処理 private function renderMotion():void { for( var i :uint =0; i< 200; ++i ) { Particle( _particles[i] ).update(); } } // 後片付け処理(このあとsetupStageが呼ばれリセットされます) private function resetStage():void { for( var i :uint =0; i< 200; ++i ) { removeChild( Particle( _particles.shift() ).display ); } _particles = []; } public function loadImageTest ():void { var url:String = "http://wonderfl.kayac.com/img/common/img_h1.gif"; var iloader:ImageLoader = new ImageLoader(); iloader.onComplete = onLoadImage; iloader.load( url ); } public function onLoadImage( img:DisplayObject ) :void { } //-------------------------------------- // よく使う値を用意する //-------------------------------------- public const ZERO:Point =new Point(); //-------------------------------------- // フレームワーク設定 //-------------------------------------- private var _textLabel:TextField; private var _btn:RectButton; private var _resetBtn:RectButton; // コンストラクタ public function Template() { init(); } // 各種要素の生成 private function init():void { var W :Number= stage.stageWidth; var H :Number= stage.stageHeight; _textLabel = new TextField(); _textLabel.filters = [ new GlowFilter( 0xFFFFFF, 1.0, 2, 2, 5 ) ]; _btn = new RectButton( W, H ); _btn.onClick = startMotion; _resetBtn = new RectButton( 40, 20, 0xFFFFFF00); _resetBtn.label = "REST"; _resetBtn.x = W- _resetBtn.width; _resetBtn.onClick = onClcikReset; initStage(); } // 画面要素の初期化 public function initStage():void { _textLabel.text = "init"; var W :Number= stage.stageWidth; var H :Number= stage.stageHeight; setupStage( W, H ); addChild( _textLabel ); addChild( _btn ); addChild( _resetBtn ); } // コントローラ public function startMotion():void {trace("startMotion"); addEventListener( Event.ENTER_FRAME, enterRender ); _textLabel.text = "rendering..."; _btn.onClick = stopMotion; } public function enterRender(e:Event):void { renderMotion(); } public function stopMotion():void {trace("stopMotion"); removeEventListener( Event.ENTER_FRAME, enterRender ); _textLabel.text = "standby."; _btn.onClick = startMotion; } // リセット public function onClcikReset ():void { stopMotion(); resetStage(); initStage(); } } import flash.events.*; import flash.display.*; import flash.geom.*; import flash.filters.*; import flash.text.*; import flash.utils.*; } import flash.events.*; import flash.display.*; import flash.geom.*; import flash.filters.*; import flash.net.*; import flash.text.*; import flash.utils.*; import flash.system.*; //-------------------------------------- // 四角 //-------------------------------------- class Rect extends Sprite { public function Rect ( c:uint = 0xFFFF0000, width:Number= 100, height:Number=100 ){ drawRect( c, width, height ) } protected function drawRect( c:uint, width:Number, height:Number ):void { graphics.beginFill( c & 0xFFFFFF, c >> 24 & 0xFF ); graphics.drawRect( 0, 0, width, height ); graphics.endFill(); } } //-------------------------------------- // 丸 //-------------------------------------- class Ball extends Sprite { public function Ball( r:Number= 5, c:uint = 0xFFFF0000 ){ drawCircle( r, c ); } protected function drawCircle( r:Number, c:uint ) :void { graphics.beginFill( c & 0xFFFFFF, c >> 24 & 0xFF ); graphics.drawCircle( 0, 0, r ); graphics.endFill(); } } //-------------------------------------- // 四角いボタン //-------------------------------------- class RectButton extends Rect { public var onClick :Function; private var _width:Number; private var _height:Number; private var _color:uint; private var _labelField :TextField; public function RectButton( w:Number, h:Number, rcolor:uint= 0x00 ) { super( rcolor, w, h ); _color = rcolor; _width = w; _height = h; buttonMode = true; useHandCursor = true; mouseChildren = false; _labelField = new TextField(); _labelField.width = w; _labelField.height = h; _labelField.selectable = false; _labelField.autoSize= TextFieldAutoSize.LEFT; addChild( _labelField); addEventListener( MouseEvent.CLICK, onClcikReset ); } private function onClcikReset ( e:Event ):void { if( onClick != null ) onClick (); } public function set label( value:String ) :void { _labelField.text = value; updateDisplay(); } public function get label() :String { return _labelField .text; } private function updateDisplay():void { drawRect( _color, Math.max( _width, _labelField.width ), _height ); } } //-------------------------------------- // Proxy経由ローダー //-------------------------------------- class ImageLoader { public static const PROXY:String = "http://5ivestar.org/proxy/"; public static const CROSSDOMAIN:String = "http://5ivestar.org/proxy/crossdomain.xml"; public var onComplete:Function; public function load( imagePath:String ) :void { Security.loadPolicyFile(CROSSDOMAIN); var loader:Loader = new Loader(); loader.contentLoaderInfo.addEventListener( Event.COMPLETE, onLoadImage ); loader.load( new URLRequest( PROXY+imagePath ) ); } private function onLoadImage( e:Event ) :void { var img :DisplayObject = Loader( LoaderInfo(e.currentTarget).loader ).content; if( onComplete != null ) onComplete( img ); } } //-------------------------------------- // 超基本パーティクル //-------------------------------------- class Particle { private var _display:DisplayObject; private var _vx:Number; private var _vy:Number; private var _life:Number; private var _friction:Number = 0.95; public function Particle( display:DisplayObject, life:Number = 60, vx:Number = 0, vy:Number = 0 ) { _display= display; _vx = vx; _vy = vy; _life = life; } public function update():void { if( --_life == 0 ) return; _vx *= _friction; _vy *= _friction; _display.x += _vx; _display.y += _vy; } public function isDead():Boolean { return _life == 0; } public function get display():DisplayObject { return _display; } } 実験Flash用テンプレートを作ってみた。改