RetroWeb ~iPhoneとAndroidでハイブリッドゲームアプリ開発~

AIR for iOS+AndroidでSmartPhone Game ハイブリッドアプリ開発

最近作ったアプリ
100億本の抜け毛 100億匹のモナー DQ3闘技場アプリ モナーペット(進化)

AIRのパフォーマンスと開発速度を上げる、ランタイムスプライトシート生成メソッド

SpriteSheet生成回りはなにかと面倒、とはいえ旧来のtimelineのフレームアニメは、作るの楽だけどスマフォだと重いうえにメモリも消費。

そこでプログラム実行時に、フレームアニメのMovieClipをテクスチャアトラス化して、アニメーションするメソッドを作りました。あとでしまむらくんゲームアプリにも実装してみよう。

	
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.events.Event;

	
function convertFrameToBMDAnime(m:MovieClip):Object{
	const TOTAL:uint = m.totalFrames;
	var totalW:Number = 0;
	var totalH:Number = 0;
	var maxW:Number = 0;
	var nowRowTotalW:Number = 0;
	var nowRowMaxH:Number = 0;
	var maxH:Number = 0;
	var w:Number;
	var h:Number;
	for(var i:uint=0; i<TOTAL; i++){
		m.gotoAndStop(i+1);
		w = m.width;
		h = m.height;
		if(nowRowTotalW + w > 1024){
			if(nowRowTotalW > totalW){
				totalW = nowRowTotalW;
			}
			totalH += nowRowMaxH;
			nowRowTotalW = 0;
			nowRowMaxH = 0;
		}
		nowRowTotalW += w;
		if(maxW < w){
			maxW = w;
		}
		if(maxH < h){
			maxH = h;
		}
		if(nowRowMaxH < h){
			nowRowMaxH = h;
		}
		if(i == TOTAL -1){
			totalH += nowRowMaxH;
		}
	}
	if(nowRowTotalW > totalW){
		totalW = nowRowTotalW;
	}
	
	//母体となるBitmapDataの作成。
	var bmd:BitmapData = new BitmapData(totalW, totalH, true, 0);
	var rects:Vector.<Rectangle> = new Vector.<Rectangle>(TOTAL, true);
	//var points:Vector.<Point> = new Vector.<Point>(TOTAL, true);
	var mat:Matrix = new Matrix();
	var x:Number = 0;
	var y:Number = 0;
	nowRowMaxH = 0;
	m.gotoAndStop(1);
	for(var i2:uint=0; i2<TOTAL; i2++){
		m.gotoAndStop(i2+1);
		w = m.width;
		h = m.height;
		if(x+w> 1024){
			x = 0;
			y += nowRowMaxH;
			nowRowMaxH = 0;
		}
		
		rect = m.getBounds(m);
		mat.tx = x - rect.x;
		mat.ty = y - rect.y;

		bmd.draw(m, mat);//, null, null, null,  true);
		if(nowRowMaxH < h){
			nowRowMaxH = h;
		}

		rects[i2] = new Rectangle(x, y, w, h);
		//points[i2] = new Point(maxW - w, h - maxH);
		x+=w;
	}

	var bmd2:BitmapData = new BitmapData(maxW, maxH, true, 0);
	var bmp:Bitmap = new Bitmap(bmd2);
	//アニメ処理
	var animeIndex:uint = 0;
	var point:Point = new Point();
	var rect:Rectangle = new Rectangle(0, 0, maxW, maxH);
	
	function nextFrame():void{
		bmd2.fillRect(rect, 0);//0xFF000000);
		//bmd2.copyPixels(bmd, rects[animeIndex], points[animeIndex]);
		bmd2.copyPixels(bmd, rects[animeIndex], point);
		animeIndex++

		if(TOTAL == animeIndex){
			animeIndex = 0;
		}
	}
	
	function end():void{
		bmd.dispose();
		bmd2.dispose();
	}
	
	return { bmp:bmp, end:end, nextFrame:nextFrame };
}

var o:Object = convertFrameToBMDAnime(TestMc);	
TestMc.parent.removeChild(TestMc);

addChild(o.bmp);//使い終わったら、removeChildした後、o.end();
addEventListener(Event.ENTER_FRAME, function(e:Event):void{ o.nextFrame(); });