var screenWidth:int = stage.fullScreenWidth;
var screenHeight:int = stage.fullScreenHeight;
var viewPort:Rectangle = new Rectangle();
//判断高宽比,可以这样理解,我们设定的比率是480/320=1.5,假如分母不变,如果分子小,则比例小,我们就以短的那边作为限制。
//如小米的分辨率854*480,我们化成同分母的话,将会变成这样的比例,569/320跟480/320比较。
//屏幕过长,我们等比缩放后,则会在上下有黑色边块,但依然适应宽,不过这样已经很好的解决了我们的问题
//如果非得满屏,你得准备大量的素材,适应不同设备,但这样做不现实。
if (screenHeight / screenWidth < Constants.ASPECT_RATIO)
{
viewPort.height = screenHeight;
viewPort.width = int(viewPort.height / Constants.ASPECT_RATIO);
viewPort.x = int((screenWidth - viewPort.width) / 2);
}
else
{
viewPort.width = screenWidth;
viewPort.height = int(viewPort.width * Constants.ASPECT_RATIO);
viewPort.y = int((screenHeight - viewPort.height) / 2);
}
//启动Starling
mStarling = new Starling(Game, stage, viewPort, null, "auto", "baseline");
mStarling.start();
//程序被激活
NativeApplication.nativeApplication.addEventListener(Event.ACTIVATE,
function (e:Event):void { mStarling.start(); });
//程序未激活
NativeApplication.nativeApplication.addEventListener(Event.DEACTIVATE,
function (e:Event):void { mStarling.stop(); });
}
}
}
复制代码
StarlingForIOS.as
package
{
import flash.desktop.NativeApplication;
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.geom.Rectangle;
import starling.core.Starling;
[SWF(frameRate="60", backgroundColor="#000")]
public class StarlingForIOS extends Sprite
{
private var mStarling:Starling;
public function StarlingForIOS()
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
//开启多点触控
Starling.multitouchEnabled = true;
//在iOS上不用开启,借阅大量内存
Starling.handleLostContext = false;
//在iPhone中能铺满屏幕;iPad中则两边会有很块,因为它们的宽高比不一样。
var screenWidth:int = stage.fullScreenWidth;
var screenHeight:int = stage.fullScreenHeight;
var viewPort:Rectangle = new Rectangle();
if (screenHeight / screenWidth < Constants.ASPECT_RATIO) { viewPort.height = screenHeight; viewPort.width = int(viewPort.height / Constants.ASPECT_RATIO); viewPort.x = int((screenWidth - viewPort.width) / 2); } else { viewPort.width = screenWidth; viewPort.height = int(viewPort.width * Constants.ASPECT_RATIO); viewPort.y = int((screenHeight - viewPort.height) / 2); } //当Stage3D层初始化,屏幕会很黑屏 //为了解决这个问题,我们可以当程序启动时采取展示一张图片,然后当Straling开始工作移除它,。 //因为我们不需要添加,Default.png之类的 var startupImage:Sprite = createStartupImage(viewPort, screenWidth > 320);
addChild(startupImage);
//启动Starling
mStarling = new Starling(Game, stage, viewPort, null, "auto", "baseline");
mStarling.stage3D.addEventListener(Event.CONTEXT3D_CREATE, function(e:Event):void
{
//Starling开始启动,移除图片
removeChild(startupImage);
mStarling.start();
});
NativeApplication.nativeApplication.addEventListener(Event.ACTIVATE,
function (e:Event):void { mStarling.start(); });
NativeApplication.nativeApplication.addEventListener(Event.DEACTIVATE,
function (e:Event):void { mStarling.stop(); });
}
//创建开始载入图像
private function createStartupImage(viewPort:Rectangle, isHD:Boolean):Sprite
{
var sprite:Sprite = new Sprite();
var background:Bitmap = isHD ?
new AssetEmbeds_2x.Background() : new AssetEmbeds_1x.Background();
var loadingIndicator:Bitmap = isHD ?
new AssetEmbeds_2x.Loading() : new AssetEmbeds_1x.Loading();
background.smoothing = true;
sprite.addChild(background);
loadingIndicator.smoothing = true;
loadingIndicator.x = (background.width - loadingIndicator.width) / 2;
loadingIndicator.y = background.height * 0.75;
sprite.addChild(loadingIndicator);
sprite.x = viewPort.x;
sprite.y = viewPort.y;
sprite.width = viewPort.width;
sprite.height = viewPort.height;
return sprite;
}
}
}
复制代码
7.编辑我们的Starling入口类的代码:Game.as
package
{
import starling.core.Starling;
import starling.display.Image;
import starling.display.Sprite;
import starling.events.Event;
import starling.events.TouchEvent;
import starling.events.TouchPhase;
public class Game extends Sprite
{
private var mBackground:Image;
private var mLogo:Image;
public function Game()
{
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage(e:Event):void
{
init();
}
private function init():void
{
//默认舞台大小,当viewPort的大小被定义才改变
stage.stageWidth = Constants.STAGE_WIDTH;
stage.stageHeight = Constants.STAGE_HEIGHT;
//渲染区域与舞台大小比率设置,不同设备Starling.current.contentScaleFactor的值不同
Assets.contentScaleFactor = Starling.current.contentScaleFactor;
trace(Starling.current.contentScaleFactor);
//准备素材
Assets.prepareSounds();
Assets.loadBitmapFonts();
//加入内容
mBackground = new Image(Assets.getTexture("Background"));
addChild(mBackground);
mLogo = new Image(Assets.getAtlasTexture("logo"));
mLogo.addEventListener(TouchEvent.TOUCH, onLogoTouched);
mLogo.x = int((Constants.STAGE_WIDTH - mLogo.width) / 2);
mLogo.y = int((Constants.STAGE_HEIGHT - mLogo.height) / 2);
addChild(mLogo);
}
private function onLogoTouched(event:TouchEvent):void
{
if (event.getTouch(mLogo, TouchPhase.BEGAN))
Assets.getSound("Click").play();
}
}
}
复制代码
8.然后我们就可以打包我们的程序为.apk或.ipa文件,进行真机测试(注意:打包android和iOS要选择对应的文档类),你会发现一切会非常美好,都能适应屏幕了,只是有些设备呢,有两边黑块,如果仅仅做iOS的话,我们准备两三套素材足以让iPhone,iTouch,iPad完美呈现。
9.最后源文件发出,供大家参考!请移步
Starling分辨率适应解决方案
最新推荐文章于 2018-11-08 09:37:02 发布