2013年3月29日金曜日

Starlingコンテンツの描画領域とassetsの管理 実装編


spriteSheetは各自で用意をして下さい。
このサンプルでは、TexturePackerを利用して作成しました。

ScreenMultiResolution.as
package
{
 import flash.display.Sprite;
 import flash.display.StageAlign;
 import flash.display.StageScaleMode;
 import flash.events.Event;
 import flash.geom.Rectangle;

 import starling.core.Starling;
 import starling.utils.RectangleUtil;
 import starling.utils.ScaleMode;

 [SWF(width="320",height="480",frameRate="60",backgroundColor="#0")]
 public class ScreenMultiResolution extends Sprite
 {
  public static const STAGE_WIDTH:uint = 320;
  public static const STAGE_HEIGHT:uint = 480;

  private var viewport:Rectangle;

  public function ScreenMultiResolution()
  {
   super();

   stage.align = StageAlign.TOP_LEFT;
   stage.scaleMode = StageScaleMode.NO_SCALE;

   addEventListener(Event.ADDED_TO_STAGE,addedToStageHandler);
  }

  protected function addedToStageHandler(event:flash.events.Event):void
  {
   removeEventListener(Event.ADDED_TO_STAGE,addedToStageHandler);

   var showType:int = 0;

   if(showType == 0)
   {
    showAll();
   } else if(showType == 1) {
    showCenter();
   } else {
    return;
   }

   Starling.handleLostContext = true;
   Starling.multitouchEnabled = true;

   var st:Starling = new Starling(Main, stage, viewport);
   st.stage.stageWidth  = STAGE_WIDTH;
   st.stage.stageHeight = STAGE_HEIGHT;
   st.start();
  }

  private function showCenter():void
  {
   viewport = RectangleUtil.fit(
    new Rectangle(0, 0, stage.stageWidth,stage.stageHeight), 
    new Rectangle((stage.stageWidth >> 1) - (STAGE_WIDTH >> 1),
    (stage.stageHeight >> 1) - (STAGE_HEIGHT >> 1),
    STAGE_WIDTH,
    STAGE_HEIGHT),
    starling.utils.ScaleMode.SHOW_ALL);
  }

  private function showAll():void
  {
   viewport = RectangleUtil.fit(
    new Rectangle(0,0,stage.stageWidth,stage.stageHeight), 
    new Rectangle(0,0,stage.fullScreenWidth,stage.fullScreenHeight),
    starling.utils.ScaleMode.SHOW_ALL);
  }

 }
}
Main.as
package
{
 import flash.events.Event;
 import flash.net.URLLoader;
 import flash.net.URLRequest;

 import starling.core.Starling;
 import starling.display.Image;
 import starling.display.Quad;
 import starling.display.Sprite;
 import starling.events.Event;
 import starling.text.TextField;
 import starling.textures.Texture;
 import starling.utils.AssetManager;

 public class Main extends Sprite
 {
  private var assetManager:AssetManager;

  public function Main()
  {
   super();

   addEventListener(starling.events.Event.ADDED_TO_STAGE,addedToStageHandler);
  }

  private function addedToStageHandler(event:starling.events.Event):void
  {
   var urlLoader:URLLoader = new URLLoader();

   urlLoader.addEventListener(flash.events.Event.COMPLETE,urlLoaderCompleteHandler);
   urlLoader.load(new URLRequest("./assets/setting.json"));
  }

  protected function urlLoaderCompleteHandler(event:flash.events.Event):void
  {
   var urlLoader:URLLoader = event.target as URLLoader;

   urlLoader.removeEventListener(flash.events.Event.COMPLETE,urlLoaderCompleteHandler);
   addAssets(JSON.parse(urlLoader.data as String));
  }

  private function addAssets(data:Object):void
  {
   var sheetPngName:String = "sheetPng";
   var sheetXmlName:String = "sheetXml";
   var scaleFactorName:String = "scaleFactor";
   var contentScaleFactor:int;
   var sScale:String;

   if(Starling.contentScaleFactor >= 1 && Starling.contentScaleFactor < 3)
   {
    contentScaleFactor = int(Starling.contentScaleFactor);
    sScale = "x"+contentScaleFactor.toString();

    assetManager = new AssetManager(contentScaleFactor);
    assetManager.verbose = true;
    assetManager.enqueue(data[sScale][sheetPngName]);
    assetManager.enqueue(data[sScale][sheetXmlName]);
    assetManager.loadQueue(loadAssetsHandler);
   }
  }

  private function loadAssetsHandler(ratio:Number):void
  {
   if(ratio < 1.0) return;

   addTextures();
  }

  private function addTextures():void
  {
   var scaleFactor:Number = Starling.current.contentScaleFactor;
   var quadW:Number = 100;
   var quadH:Number = 100;

   var bg:Quad = makeQuad(0,0,stage.stageWidth,stage.stageHeight,0x0000ff);
   bg.alpha = 0.5;
   addChild(bg);

   var luQuad:Quad = makeQuad(0,0,100,100,Math.random() * 0xffffff);
   addChild(luQuad);

   var ldQuad:Quad = makeQuad(0,stage.stageHeight - quadH,100,100,Math.random() * 0xffffff);
   addChild(ldQuad);

   var ruQuad:Quad = makeQuad(stage.stageWidth - quadW,0,100,100,Math.random() * 0xffffff);
   addChild(ruQuad);

   var rdQuad:Quad = makeQuad(stage.stageWidth - quadW,stage.stageHeight - quadH,
    100,100,Math.random() * 0xffffff);
   addChild(rdQuad);

   var texture:Texture = assetManager.getTexture("bird");

   var image:Image = new Image( texture );
   addChild(image);
   image.x = 0;
   image.y = 0;

   var image2:Image = new Image( texture );
   addChild(image2);
   image2.x = stage.stageWidth - image.width;
   image2.y = (stage.stageHeight) - (image.height);

   var image3:Image = new Image( texture );
   addChild(image3);
   image3.x = (stage.stageWidth >> 1) - (image.width >> 1);
   image3.y = (stage.stageHeight >> 1) - (image.height >> 1);
  }

  private function makeQuad(_x:Number,_y:Number,_w:Number,_h:Number,_color:Number):Quad
  {
   var quad:Quad = new Quad(_w,_h,_color);
   quad.x = _x;
   quad.y = _y;

   return quad;
  }

 }
}
setting.json
{"x1":{"sheetPng":"./assets/spriteSheet.png","sheetXml":"./assets/spriteSheet.xml"},"x2":{"sheetPng":"./assets/spriteSheet2x.png","sheetXml":"./assets/spriteSheet2x.xml"}}


0 件のコメント:

コメントを投稿