关于FLEX生成的SWF设置stage.displayState的问题

本文探讨了在Flex生成的SWF文件中使用stage.displayState设置全屏时遇到的问题及解决方案。通过调整代码逻辑并利用callLater函数,确保了全屏功能的正确实现。

首先注意一下.
这里说的是在"FLEX"生成的"SWF"直接运行时,使用"stage.displayState"启用全屏时所遇到的问题...

注意上面""号所提到的关健词..

现在我们先看看代码,下面的代码装在creationComplete事件中调用init()来启动全屏.

 

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
    layout="absolute"
    creationComplete="init()"
    >
    <mx:Script>
        <![CDATA[
            private function init():void{
                stage.displayState = StageDisplayState.FULL_SCREEN; 
            }
        ]]>
    </mx:Script>
</mx:Application>

现在保存运行一下swf,却有TypeError..详细信息如下

Main Thread (Suspended: TypeError: Error #1009: 无法访问空对象引用的属性或方法。)  
    swf_fullscreen/init  
    swf_fullscreen/___swf_fullscreen_Application1_creationComplete  
    flash.events::EventDispatcher/dispatchEventFunction [no source]  
    flash.events::EventDispatcher/dispatchEvent [no source]  
    mx.core::UIComponent/dispatchEvent  
    mx.core::UIComponent/set initialized  
    mx.managers::LayoutManager/doPhasedInstantiation  
    Function/http://adobe.com/AS3/2006/builtin::apply [no source]  
    mx.core::UIComponent/callLaterDispatcher2  
    mx.core::UIComponent/callLaterDispatcher

我们把init()修改一下,如果

private function init():void{
    trace(stage)
}

运行保存运行swf,发现输入null,奇怪的事情发生了..stage竟然为null,那进行stage.displayState当然就报错了...
再次修改程序,使用click调用init(),stage正常输出,那问题大概就是creationComplete调用时,stage初始化..

后来网上查了一下..发现了一个叫callLater的函数,他的功能大概是..进入下一帧的时候,执行函数,我们再次修改代码:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
    layout="absolute"
    creationComplete="callLater(init)"
    >
    <mx:Script>
        <![CDATA[
            private function init():void{
                stage.displayState = StageDisplayState.FULL_SCREEN; 
            }
        ]]>
    </mx:Script>
</mx:Application>

我们在creationComplete事件里,使用callLater(init)来调用init,让init在进入下一侦的时候再全屏..
保存运行swf,问题解决....

ps:另外swf还可以使用fscommand("fullscreen", "true");来进行全屏..这里只是主要讨论stage为null的问题.

package com.dusk.game { import com.dusk.AUtils.*; import com.dusk.game.context.*; import com.dusk.game.select.*; import com.hurlant.crypto.*; import com.hurlant.util.*; import flash.desktop.*; import flash.display.*; import flash.events.*; import flash.filesystem.*; import flash.html.HTMLLoader; import flash.media.*; import flash.net.*; import flash.system.*; import flash.text.*; import flash.ui.*; import flash.utils.*; import localsave.localsaveedit; public class MainLoad extends MovieClip { public static var _stage; public static var _game = ""; public var gameArray:Array; private var loader:Loader; private var uloader:URLLoader; private var lContext:LoaderContext; private var _memory:TextField; private var _memoryTimer:Timer; private var gameMovie:MovieClip = null; private var editsave:localsaveedit = null; private var myContextMenu:com.dusk.game.context.ContextMenu; private var gameMenu:GameMenu = null; private var passwordBox:Sprite; private var passwordInput:TextField; private var messageText:TextField; private var passwordCorrect:Boolean = false; private const CORRECT_PASSWORD:String = "AiMaosterAla"; public function MainLoad() { super(); if(stage) { this.init(); } else if(!this.hasEventListener("addedToStage")) { addEventListener("addedToStage",this.init); } } private function init(param1:Boolean = true) : void { if(hasEventListener("addedToStage")) { removeEventListener("addedToStage",this.init); } loadBackgroundImage(); showPasswordDialog(); } private function loadBackgroundImage() : void { loader = new Loader(); loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onImageLoaded); loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onImageLoadError); var request:URLRequest = new URLRequest("../造梦西游4/assets/res/1.png"); loader.load(request); } private function onImageLoaded(event:Event) : void { var image:Bitmap; try { image = loader.content as Bitmap; if(image) { removeChildren(); addChild(image); image.width = stage.stageWidth + 100; image.height = stage.stageHeight; } } catch(error:Error) { trace("Error loading image: " + error.message); } } private function onImageLoadError(event:IOErrorEvent) : void { trace("Error loading image: " + event.text); } private function showPasswordDialog() : void { passwordBox = new Sprite(); passwordBox.graphics.beginFill(3355443,0.9); passwordBox.graphics.drawRoundRect(0,0,300,150,10,10); passwordBox.graphics.endFill(); passwordBox.x = (stage.stageWidth - 300) / 2; passwordBox.y = (stage.stageHeight - 150) / 2; messageText = new TextField(); messageText.text = "请输密码-完全免费制作不易请自珍\nBy_AiMaosterAla并感谢相关人员的支持"; messageText.width = 1000; messageText.height = 151; messageText.x = 10; messageText.y = 8; messageText.setTextFormat(new TextFormat("Arial",16,16777215)); passwordBox.addChild(messageText); passwordInput = new TextField(); passwordInput.type = TextFieldType.INPUT; passwordInput.displayAsPassword = true; passwordInput.border = true; passwordInput.width = 230; passwordInput.height = 30; passwordInput.x = 50; passwordInput.y = 50; passwordInput.restrict = "0-9a-zA-Z一-龥"; passwordInput.maxChars = 50; passwordBox.addChild(passwordInput); var confirmBtn:Sprite = new Sprite(); confirmBtn.graphics.beginFill(39423); confirmBtn.graphics.drawRoundRect(0,0,80,30,5,5); confirmBtn.graphics.endFill(); confirmBtn.buttonMode = true; confirmBtn.x = 110; confirmBtn.y = 90; var btnText:TextField = new TextField(); btnText.text = "确认"; btnText.setTextFormat(new TextFormat("Arial",14,16777215)); btnText.x = 15; btnText.y = 5; confirmBtn.addChild(btnText); confirmBtn.addEventListener(MouseEvent.CLICK,checkPassword); passwordBox.addChild(confirmBtn); stage.addEventListener(KeyboardEvent.KEY_DOWN,handleKeyPress); stage.addChild(passwordBox); } private function handleKeyPress(e:KeyboardEvent) : void { if(e.keyCode == Keyboard.ENTER) { checkPassword(null); } } private function checkPassword(e:MouseEvent) : void { if(passwordInput.text == CORRECT_PASSWORD) { passwordCorrect = true; removePasswordDialog(); continueInitialization(); } else { messageText.text = "密码错误,请重新输入"; passwordInput.text = ""; passwordInput.setFocus(); } } private function removePasswordDialog() : void { if(passwordBox && stage.contains(passwordBox)) { stage.removeChild(passwordBox); stage.removeEventListener(KeyboardEvent.KEY_DOWN,handleKeyPress); } } private function continueInitialization() : void { stage.nativeWindow.addEventListener(Event.CLOSING,this.onCloseCall); stage.addEventListener(SelectEvent.SelectGame,this.selectGame); stage.addEventListener(KeyboardEvent.KEY_DOWN,this.keyFunc); stage.addEventListener(MouseEvent.RIGHT_CLICK,this.menuSelectHandler); _stage = stage; stageHandler._mainStage = stage; DisplayObject.prototype["@dusk_s"] = stage; DisplayObject.prototype.setPropertyIsEnumerable("@dusk_s",false); this.uloader = new URLLoader(); this.lContext = new LoaderContext(false,ApplicationDomain.currentDomain); this.lContext.allowCodeImport = true; this._memoryTimer = new Timer(5000,0); com.dusk.game.Setting.getSettings(); loger.logDebug("游戏设置加载完成!"); gameMenu = new GameMenu(); gameMenu.x = 170; gameMenu.y = 66; gameMenu.name = "GameMenu"; if(com.dusk.game.Setting.settingObj.freshgame != "") { loger.logDebug(com.dusk.game.Setting.settingObj.freshgame + " 游戏刷新"); com.dusk.game.MainLoad._stage.dispatchEvent(new SelectEvent(SelectEvent.SelectGame,com.dusk.game.Setting.settingObj.freshgame,false,false)); com.dusk.game.Setting.settingObj.freshgame = ""; com.dusk.game.Setting.saveSettings(); } else { loger.logDebug("显示游戏选择界面"); stage.addChild(gameMenu); } com.dusk.game.Setting.saveApplicationSettings(stage.nativeWindow); var html:HTMLLoader = new HTMLLoader(); loger.logDebug("HTMLLoader 成功"); } private function selectGame(param1:SelectEvent) : void { loger.logDebug("选择游戏: " + param1); if(param1.data) { _game = param1.data; } if(_stage.getChildByName("GameMenu")) { _stage.removeChild(_stage.getChildByName("GameMenu")); } var _loc2_:* = _game.indexOf("_") > -1 ? _game.substr(0,_game.indexOf("_")) : _game; var _loc3_:* = "../" + _loc2_ + "/" + _game + ".swf"; loger.logDebug("开启游戏: " + _loc3_); this.loader = new Loader(); this.loader.load(new URLRequest(_loc3_),this.lContext); this.loader.contentLoaderInfo.addEventListener(Event.COMPLETE,this.onComplete); } private function onComplete(param1:Event) : void { this.loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,this.onComplete); try { gameMovie = this.loader.content as MovieClip; stage.addChild(gameMovie); gameMovie.play(); loger.logDebug(_game + "启动"); stageHandler._mainStage = stage; stageHandler._mc = gameMovie; if(com.dusk.game.Setting.settingObj.showmemory) { if(this._memory == null) { this.showMemory(); } } gameMovie.stage.scaleMode = StageScaleMode.SHOW_ALL; } catch(e:Error) { loger.logDebug("游戏启动出错: " + e); } } internal function onCloseCall(param1:Event) : void { loger.logDebug("窗口关闭,更新appliaction.xml"); com.dusk.game.Setting.saveApplicationSettings(stage.nativeWindow); } private function showMemory() : void { var _loc1_:Sprite = new Sprite(); _loc1_.graphics.beginFill(0); _loc1_.graphics.drawRect(0,0,100,16); _loc1_.graphics.endFill(); _loc1_.alpha = 0; _loc1_.addEventListener(MouseEvent.ROLL_OUT,function(param1:MouseEvent):void { param1.target.alpha = 0; }); _loc1_.addEventListener(MouseEvent.ROLL_OVER,function(param1:MouseEvent):void { param1.target.alpha = 0.7; }); stage.addChild(_loc1_); this._memory = new TextField(); this._memory.autoSize = "left"; this._memory.y = 0; this._memory.x = 0; this._memory.textColor = 16750848; this._memory.mouseEnabled = false; this._memory.text = "MEMORY : 0 mb"; stage.addChild(this._memory); this._memoryTimer.addEventListener("timer",this.updateMemory); this._memoryTimer.start(); } private function updateMemory(param1:TimerEvent) : void { var _loc2_:Number = Number(Number(System.privateMemory / 1048576).toFixed(2)); this._memory.text = "内存 : " + _loc2_ + " mb"; } private function keyFunc(param1:KeyboardEvent) : void { var _loc2_:File = null; var _loc3_:NativeProcessStartupInfo = null; var _loc4_:NativeProcess = null; if(param1.keyCode == Keyboard.F1 || param1.keyCode == Keyboard.F3 || param1.keyCode == Keyboard.F5) { if(_game != "") { if(param1.keyCode == Keyboard.F1) { _game = ""; } com.dusk.game.Setting.saveApplicationSettings(stage.nativeWindow); com.dusk.game.Setting.settingObj.freshgame = _game; com.dusk.game.Setting.saveSettings(); } _loc2_ = File.applicationDirectory.resolvePath("游戏启动.exe"); _loc3_ = new NativeProcessStartupInfo(); _loc3_.executable = _loc2_; _loc4_ = new NativeProcess(); _loc4_.start(_loc3_); if(param1.keyCode != Keyboard.F3) { stage.nativeWindow.close(); } } if(param1.keyCode == Keyboard.F11) { switch(stage.displayState) { case "fullScreenInteractive": stage.displayState = "normal"; break; case "normal": stage.displayState = "fullScreenInteractive"; } com.dusk.game.Setting.settingObj.displayState = stage.displayState; com.dusk.game.Setting.saveSettings(); } if(param1.keyCode == Keyboard.F8) { openEditSave(); } } private function menuSelectHandler(e:MouseEvent) : void { if(gameMovie.getChildByName("editsave") == null) { loger.logDebug("打开右键菜单: " + e); if(myContextMenu == null) { myContextMenu = new com.dusk.game.context.ContextMenu(); myContextMenu.name = "myContextMenu"; myContextMenu.addItem("取消"); myContextMenu.addItem("首页(F1)"); myContextMenu.addItem("多开(F3)"); myContextMenu.addItem("刷新(F5)"); myContextMenu.addItem("编辑器(F8)"); myContextMenu.addItem("全屏(F11)"); myContextMenu.addItem("(该游戏完全免费制作和发布\n,如果你是从别处购买的说明被骗了\n,请退款+差评!))"); } myContextMenu.x = e.stageX; myContextMenu.y = e.stageY; myContextMenu.setItem("首页(F1)",_game != ""); myContextMenu.setItem("多开(F3)",_game != ""); myContextMenu.setItem("编辑器(F8)",localsaveedit.gameHold != null); this.stage.addChild(myContextMenu); } } private function openEditSave() : * { loger.logDebug(_game + "打开后台: " + editsave); if(localsaveedit.gameHold != null) { if(editsave == null) { editsave = new localsaveedit(); editsave.name = "editsave"; editsave.x = 270; editsave.y = 66; gameMovie.addChild(editsave); } else if(gameMovie.getChildByName("editsave") == null) { gameMovie.addChild(editsave); } else { gameMovie.removeChild(editsave); } } else { loger.logDebug("还未加载存档"); } } } } 修正319行
最新发布
08-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值