把 Flash 網頁遊戲移植到 Android 的 AIR 環境時,會遇到一類「在 Flash Player 上正常、在 AIR 上卻出錯」的差異。以下是我整理出的清單。
如果同一個 swf 既要支援 flash player 播放,又要支援 air,則通過類型來判斷:
if(flash.system.Capabilities.playerType !="Desktop")
Security.allowDomain("*");
var cm:ContextMenu =new ContextMenu();
air 的 cm.customItems 為空,因此原本靠右鍵選單提供的功能需要另外安排入口。
報錯:調用 loader.loadBytes() 方法時,SecurityError: Error #3226: Cannot import a SWF file when LoaderContext.allowCodeImport is false. 原因是沒有設置 LoaderContext.allowCodeImport,這樣解決:
var con:LoaderContext = new LoaderContext(); con.allowCodeImport = true; loadBytes(mc, con);
報錯:1119: 訪問可能未定義的屬性 allowCodeImport(通過 static 類型 flash.system:LoaderContext 引用)。原因是使用了 allowCodeImport 屬性,需要 flash player 10.1 才支援,請確認編譯時的 playerglobal.swc 版本。
報錯:調用 loader.load() 方法時,SecurityError: Error #2142: Security sandbox violation: local SWF files cannot use the LoaderContext.securityDomain property. 原因是設置了 LoaderContext,而 air 不需要用 new LoaderContext():
con = new LoaderContext(false,null,SecurityDomain.currentDomain); loader.load(new URLRequest(_source), con);
stage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGE,onOrientationChange);
private function onOrientationChange(event:StageOrientationEvent):void
{
switch(event.afterOrientation)
{
case StageOrientation.DEFAULT: // re-orient display objects based on
// the default(right-side up) orientation.
stage.setOrientation(StageOrientation.DEFAULT)
break;
case StageOrientation.ROTATED_RIGHT: // Re-orient display objects based on
// right-hand orientation.
stage.setOrientation(StageOrientation.ROTATED_RIGHT)
break;
case StageOrientation.ROTATED_LEFT: // Re-orient display objects based on
// left-hand orientation.
stage.setOrientation(StageOrientation.ROTATED_LEFT)
break;
case StageOrientation.UPSIDE_DOWN: // Re-orient display objects based on
// upside-down orientation.
stage.setOrientation(StageOrientation.UPSIDE_DOWN)
break;
}
}
到目前為止,好像還不能實現手機麥克風(Microphone.isSupported 為 true,但不知為何說話沒反應,待解決)。這類問題通常與 AIR 的權限宣告或 mic 增益設定有關,可以先試 Microphone.getMicrophone().gain 是否為 0。
修改 -app.xml 文件的 icon 屬性:
<icon> <image16x16>ico/16.png</image16x16> <image32x32></image32x32> <image36x36></image36x36> <image48x48></image48x48> </icon>
這裡的空白節點要記得填,否則低解析度裝置上會退回預設圖示。整體而言, AIR 移植最花時間的往往不是語法,而是這些「桌面與行動沙箱規則不同」所引發的行為差異,建議把 Capabilities.playerType 判斷集中管理,避免散落各處難以維護。