现在的位置: 首页 > 综合 > 正文

flex的stage(转载)

2014年02月21日 ⁄ 综合 ⁄ 共 2643字 ⁄ 字号 评论关闭

现在我们先看看代码,下面的代码装在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..详细信息如下

view plaincopy to clipboardprint?

  1. Main Thread (Suspended: TypeError: Error #1009: 无法访问空对象引用的属性或方法。)    
  2.     swf_fullscreen/init    
  3.     swf_fullscreen/___swf_fullscreen_Application1_creationComplete     
  4.     flash.events::EventDispatcher/dispatchEventFunction [no source]     
  5.     flash.events::EventDispatcher/dispatchEvent [no source]     
  6.     mx.core::UIComponent/dispatchEvent     
  7.     mx.core::UIComponent/set initialized     
  8.     mx.managers::LayoutManager/doPhasedInstantiation    
  9. Function/http://adobe.com/AS3/2006/builtin::apply [no source]  
  10.     mx.core::UIComponent/callLaterDispatcher2     
  11.     mx.core::UIComponent/callLaterDispatcher    
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()修改一下,如果

view plaincopy to clipboardprint?

  1. private function init():void{  
  2.     trace(stage)  
  3. }  
private function init():void{
trace(stage)
}

运行保存运行swf,发现输入null,奇怪的事情发生了..stage竟然为null,那进行stage.displayState当然就报错了...

再次修改程序,使用click调用init(),stage正常输出,那问题大概就是creationComplete调用时,stage初始化..

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

view plaincopy to clipboardprint?

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
  3.     layout="absolute"
  4.     creationComplete="callLater(init)"
  5.     >  
  6.     <mx:Script>   
  7.         <![CDATA[  
  8. private function init():void{   
  9.                 stage.displayState = StageDisplayState.FULL_SCREEN;   
  10.             }   
  11.         ]]>  
  12.     </mx:Script>   
  13. </mx:Application>  
<?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的问题.

抱歉!评论已关闭.