SWFProfilerをデフォルトで表示する

testclass_swfprofiler

デバッグのお供に com.flashdynamix.utils.SWFProfiler を重宝してます。Tweensyパッケージに同梱されてるユーティリティで、FPSやメモリ使用量をグラフ表示してくれて超便利。
使い方は、SWF上で右クリックで出てくるコンテキストメニューに追加される"Show Profiler"から表示を切り替えるんだけど、デフォルトで表示しないことになってる。
ちょろっと手を加えてデフォルトでも表示できるように SWFProfiler.init() 関数を変更してみた。

  • ActionScript
  • SWFProfiler.as
  • Source
public static function init(context : InteractiveObject, display:Boolean = false/* ←ココ */) : void {
	if(inited) return;

	inited = true;
	stage = context.stage;

	content = new ProfilerContent();
	frame = new Sprite();

	minFps = Number.MAX_VALUE;
	maxFps = Number.MIN_VALUE;
	minMem = Number.MAX_VALUE;
	maxMem = Number.MIN_VALUE;

	var cm : ContextMenu = new ContextMenu();
	cm.hideBuiltInItems();
	ci = new ContextMenuItem("Show Profiler", true);
	cm.customItems = [ci];
	context.contextMenu = cm;
	addEvent(ci, ContextMenuEvent.MENU_ITEM_SELECT, onSelect);

	start();
	if (display) show();	/* ←ココ */
}
  1. public static function init(context : InteractiveObject, display:Boolean = false/* ←ココ */) : void {
  2.     if(inited) return;
  3.  
  4.     inited = true;
  5.     stage = context.stage;
  6.  
  7.     content = new ProfilerContent();
  8.     frame = new Sprite();
  9.  
  10.     minFps = Number.MAX_VALUE;
  11.     maxFps = Number.MIN_VALUE;
  12.     minMem = Number.MAX_VALUE;
  13.     maxMem = Number.MIN_VALUE;
  14.  
  15.     var cm : ContextMenu = new ContextMenu();
  16.     cm.hideBuiltInItems();
  17.     ci = new ContextMenuItem("Show Profiler", true);
  18.     cm.customItems = [ci];
  19.     context.contextMenu = cm;
  20.     addEvent(ci, ContextMenuEvent.MENU_ITEM_SELECT, onSelect);
  21.  
  22.     start();
  23.     if (display) show();    /* ←ココ */
  24. }

この二箇所の変更で、右クリックから表示を切り替えなくても SWFProfiler.init() の第二引数に true を渡してあげればデフォルトで表示されるようになる。もちろんその後、右クリックから"Hide Profiler"で消すこともできる。
制作の課程でロジック実装とか検証とかデバッグ中は true にしとけば一々右クリックしなくていいから楽。で、制作が進むにつれ false にして、リリース時には SWFProfiler を消すと。

  • ActionScript
  • TestClass_SWFProfiler.as
  • Source
package {
	import com.flashdynamix.motion.Tweensy;
	import com.flashdynamix.motion.TweensyTimeline;
	import com.flashdynamix.utils.SWFProfiler;
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;

	[SWF(backgroundColor = 0x000000, width = 600, height = 400, frameRate = 30)]
	public class TestClass_SWFProfiler extends Sprite {

		public function TestClass_SWFProfiler() {
			SWFProfiler.init(this, true);

			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}

		private function onEnterFrame(e:Event):void {
			var star:Shape = createStar();
			var stageWidth:Number = stage.stageWidth;
			var stageHeight:Number = stage.stageHeight;

			star.x = stageWidth / 2;
			star.y = stageHeight / 2;

			var targetScale:Number = Math.random() * 5 + 5;
			var tweensyTimeline:TweensyTimeline = Tweensy.to(
				star, {
					x: Math.random() * stageWidth,
					y: Math.random() * stageHeight,
					scaleX: targetScale,
					scaleY: targetScale,
					rotation: -360 + Math.random() * 720,
					alpha: 0
				}, Math.random() * 2 + 1
			);
			tweensyTimeline.onComplete = onTweensyComplete;
			tweensyTimeline.onCompleteParams = [tweensyTimeline, star];

			addChildAt(star, 0);
		}

		private function onTweensyComplete(tweensyTimeline:TweensyTimeline, star:Shape):void {
			tweensyTimeline.dispose();
			removeChild(star);
		}

		private function createStar():Shape {
			var star:Shape = new Shape();
			var g:Graphics = star.graphics;
			var commands:Vector. = new Vector.(10, true);
			var data:Vector. = new Vector.(20, true);

			var i:int, radius:Number, angle:Number, coord:Point;
			var innerRadius:Number = Math.random() * 30;
			var outerRadius:Number = innerRadius + 10 + Math.random() * 10;
			var unit:Number = Math.PI / 5;
			for (i = 0; i < 10; i ++) {
				commands[i] = (i == 0)? 1: 2;

				radius = (i % 2 == 0)? outerRadius: innerRadius;
				angle = -Math.PI / 2 + unit * i;
				coord = Point.polar(radius, angle);
				data[i * 2] = coord.x;
				data[i * 2 + 1] = coord.y;
			}

			var color:uint = Math.random() * 0xffffff;

			g.beginFill(color);
			g.drawPath(commands, data);
			g.endFill();

			return star;
		}


	}

}
  1. package {
  2.     import com.flashdynamix.motion.Tweensy;
  3.     import com.flashdynamix.motion.TweensyTimeline;
  4.     import com.flashdynamix.utils.SWFProfiler;
  5.     import flash.display.Graphics;
  6.     import flash.display.Shape;
  7.     import flash.display.Sprite;
  8.     import flash.events.Event;
  9.     import flash.geom.Point;
  10.  
  11.     [SWF(backgroundColor = 0x000000, width = 600, height = 400, frameRate = 30)]
  12.     public class TestClass_SWFProfiler extends Sprite {
  13.  
  14.         public function TestClass_SWFProfiler() {
  15.             SWFProfiler.init(this, true);
  16.  
  17.             addEventListener(Event.ENTER_FRAME, onEnterFrame);
  18.         }
  19.  
  20.         private function onEnterFrame(e:Event):void {
  21.             var star:Shape = createStar();
  22.             var stageWidth:Number = stage.stageWidth;
  23.             var stageHeight:Number = stage.stageHeight;
  24.  
  25.             star.x = stageWidth / 2;
  26.             star.y = stageHeight / 2;
  27.  
  28.             var targetScale:Number = Math.random() * 5 + 5;
  29.             var tweensyTimeline:TweensyTimeline = Tweensy.to(
  30.                 star, {
  31.                     x: Math.random() * stageWidth,
  32.                     y: Math.random() * stageHeight,
  33.                     scaleX: targetScale,
  34.                     scaleY: targetScale,
  35.                     rotation: -360 + Math.random() * 720,
  36.                     alpha: 0
  37.                 }, Math.random() * 2 + 1
  38.             );
  39.             tweensyTimeline.onComplete = onTweensyComplete;
  40.             tweensyTimeline.onCompleteParams = [tweensyTimeline, star];
  41.  
  42.             addChildAt(star, 0);
  43.         }
  44.  
  45.         private function onTweensyComplete(tweensyTimeline:TweensyTimeline, star:Shape):void {
  46.             tweensyTimeline.dispose();
  47.             removeChild(star);
  48.         }
  49.  
  50.         private function createStar():Shape {
  51.             var star:Shape = new Shape();
  52.             var g:Graphics = star.graphics;
  53.             var commands:Vector.<int> = new Vector.<int>(10, true);
  54.             var data:Vector.<number> = new Vector.<number>(20, true);
  55.  
  56.             var i:int, radius:Number, angle:Number, coord:Point;
  57.             var innerRadius:Number = Math.random() * 30;
  58.             var outerRadius:Number = innerRadius + 10 + Math.random() * 10;
  59.             var unit:Number = Math.PI / 5;
  60.             for (i = 0; i < 10; i ++) {
  61.                 commands[i] = (i == 0)? 1: 2;
  62.  
  63.                 radius = (i % 2 == 0)? outerRadius: innerRadius;
  64.                 angle = -Math.PI / 2 + unit * i;
  65.                 coord = Point.polar(radius, angle);
  66.                 data[i * 2] = coord.x;
  67.                 data[i * 2 + 1] = coord.y;
  68.             }
  69.  
  70.             var color:uint = Math.random() * 0xffffff;
  71.  
  72.             g.beginFill(color);
  73.             g.drawPath(commands, data);
  74.             g.endFill();
  75.  
  76.             return star;
  77.         }
  78.  
  79.  
  80.     }
  81.  
  82. }

ちなみにTweensyの作者のブログで公開されてるAS3 SWF Profilerのソースと、Tweensyに同梱されてるソースとでは関数の引数とか多少違うみたい。今回の解説は、Tweensyに同梱されてる方の SWFProfiler ですよ。


About this entry