2012-01-11 76 views
0

大家下午好,
我在用adobe air(flex sdk 4.6.0)創建一個android應用程序,我試圖找出手機屏幕的寬度是以像素爲單位的。所以我正在休學教程here,但是當我將代碼複製到我的項目中並單擊運行時,它總是給我這個錯誤「TypeError:Error#1009:Can not access a property or method of null object reference。」並去了stage.stageWidth我也試過stage.width但它做同樣的事情。那麼stage.stageWidth不再有效,或者你如何判斷平板電腦或手機的屏幕尺寸?它是否替換爲手機的其他東西?我正在嘗試獲取信息,以便我可以使用操作腳本將圖像集中在屏幕的中心。爲什麼在android上使用flex的flash Builder 4.6中的stage.stageWidth不起作用?

protected function init():void { 
      //function run at creationComplete="init()" 
      movePhotoToCenter(myIMG1); 
      // trying to get the stage width so that i can center a img that is allready added to the flex code and has a id of "myIMG1" 
     } 



     protected function movePhotoToCenter(img:Image):void { 
      //"error here" where im centering the image 
      var centerOfStage:Number = stage.stageWidth/2; 
      var Xnumber:Number = centerOfStage - (img.width/2); 
      trace(Xnumber); 
      var move:Move = new Move(img); 
      move.xFrom = img.x; 
      move.xTo = Xnumber; 
      move.yFrom = img.y; 
      move.yTo = img.y; 
      move.duration = 3000; 
      move.play(); 
      trace(img.width); 
      img.scaleX=1; 
      img.scaleY=1; 
      trace(img.width); 
     } 

感謝您的幫助, 賈斯汀

+1

當「DisplayObject」已添加到舞臺上時,'stage'僅爲空。您是否確定在舞臺上知道它的時候(通常是等待「ADDED_TO_STAGE」事件),在上下文中進行評估? – 2012-01-11 17:18:52

+0

我在創建完成事件中運行它。 init():void {stage.stageHeight; }我需要什麼直到事後得到舞臺降級? – Justin 2012-01-11 17:26:45

+0

編輯你的問題,並添加你的班級的相關部分 – 2012-01-11 17:29:00

回答

4

時,它已經被添加到一個顯示列表,不一定是舞臺的顯示列表中的creationComplete完整的事件觸發。 creationComplete當這個對象及其所有的孩子已被創建並添加到東西

還有另一個事件綁定到addedToStage(在flex land中),當DisplayObject實際添加到舞臺時會發生這種情況。

請注意,creationComplete只會在實例化一個對象並將其作爲子項添加到某個對象時觸發一次,其中addedToStage將在任何時候被觸發添加到舞臺上(如果它稍後被刪除並重新添加不用重建物體)。

請參閱this blog post瞭解有關該主題的更多詳細信息。

相關問題