開発環境
Adobe AIR 3.6
Starling Framework 1.3
Feathers 1.0

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.HAlign;
import starling.utils.RectangleUtil;
import starling.utils.VAlign;
[SWF(width="640",height="960",frameRate="60",backgroundColor="#0")]
public class TestiOS extends Sprite
{
public static const STAGE_WIDTH:uint = 640;
public static const STAGE_HEIGHT:uint = 960;
private var viewport:Rectangle;
public function TestiOS()
{
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);
viewport = RectangleUtil.fit(
new Rectangle(0,0,stage.stageWidth,stage.stageHeight),
new Rectangle(0,0,stage.fullScreenWidth,stage.fullScreenHeight),
starling.utils.ScaleMode.SHOW_ALL);
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();
st.showStatsAt(HAlign.RIGHT,VAlign.BOTTOM,1);
}
}
}
Main.as
package
{
import starling.display.Quad;
import starling.display.Sprite;
import starling.events.Event;
import starling.utils.Color;
public class Main extends Sprite
{
public function Main()
{
super();
addEventListener(Event.ADDED_TO_STAGE,addedToStageHandler);
}
private function addedToStageHandler(event:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE,addedToStageHandler);
var quad:Quad = new Quad(100,100,Color.BLUE);
quad.x = (stage.stageWidth >> 1) - (quad.width >> 1);
quad.y = (stage.stageHeight >> 1) - (quad.height >> 1);
addChild(quad);
}
}
}


protected var theme:MetalWorksMobileTheme;
private var popUpWindow:PopUpWindow;
private function doPopUpWindow():void
{
this.theme = new MetalWorksMobileTheme(this.stage);
popUpWindow = new PopUpWindow();
popUpWindow.addEventListener(PopUpWindow.WINDOW_CLOSE,popUpWindowCloseHandler);
popUpWindow.width = 320;
popUpWindow.height = 240;
PopUpManager.addPopUp(popUpWindow);
}
private function popUpWindowCloseHandler(event:Event):void
{
PopUpManager.removePopUp(popUpWindow);
popUpWindow.dispose();
}
package controls
{
import feathers.controls.Button;
import feathers.core.FeathersControl;
import feathers.display.Scale9Image;
import feathers.textures.Scale9Textures;
import flash.geom.Rectangle;
import starling.animation.Transitions;
import starling.core.Starling;
import starling.events.Event;
import starling.textures.Texture;
public class PopUpWindow extends FeathersControl
{
[Embed(source="./assets/scale9.png")]
private static const SCALE_9_TEXTURE:Class;
public static const WINDOW_CLOSE:String = "windowClose";
public function PopUpWindow()
{
super();
}
private var okButton:Button;
private var bg:Scale9Image;
override protected function initialize():void
{
var texture:Texture = Texture.fromBitmap(new SCALE_9_TEXTURE(), false);
var textures:Scale9Textures = new Scale9Textures(texture,new Rectangle(20, 20, 20, 20));
bg = new Scale9Image(textures, 1);
bg.width = width;
bg.height = height;
bg.x = bg.pivotX = width >> 1;
bg.y = bg.pivotY = height >> 1;
addChild(bg);
okButton = new Button();
okButton.label = "Ok";
okButton.width = 100;
okButton.height = 48
addChild(okButton);
okButton.x = (bg.width >> 1) - (okButton.width >> 1);
okButton.y = (bg.height >> 1) - (okButton.height >> 1);
okButton.addEventListener(Event.TRIGGERED, button_triggeredHandler);
Starling.juggler.tween(bg, 1,
{
transition: Transitions.EASE_OUT_BOUNCE,
onStart:tweenStartHandler,
onComplete:tweenCompleteHandler,
scaleX: 1,
scaleY: 1
});
}
private function button_triggeredHandler(event:Event):void
{
dispatchEventWith(WINDOW_CLOSE);
}
private function tweenStartHandler():void
{
okButton.visible = false;
bg.scaleX = 0;
bg.scaleY = 0;
}
private function tweenCompleteHandler():void
{
okButton.visible = true;
}
override public function dispose():void
{
bg.dispose();
okButton.dispose();
removeChildren();
super.dispose();
}
}
}
var showType:int = 0;
if(showType == 0)
{
showAll();
} else if(showType == 1) {
showCenter();
} else {
return;
}
変数showTypeの0で画面全体を表示し、1で画面の中央に表示に切り替えます。
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);
}
RectangleUtil.fit()メソッドは、第1引数の矩形領域を第2引数の矩形領域に、縦横比は変えずに納めるためのRectangleオブジェクトを返します。 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();
}
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"}}