2010-02-13 65 views
1

在我的AS3類中,我調用this.width,它返回的值始終爲1,即使在給定對象內容的情況下這是不可能的。this.width在一個類中總是返回相同的值

這是AS3的標準行爲嗎?

簡單版本的類如下所示。它附加到只包含簡單六邊形的MovieClip符號。

package { 

    import flash.display.*; 
    import flash.utils.*; 
    import flash.events.*; 

    public class Hexagon extends MovieClip 
    { 
     var startWidth:Number; 
     var startHeight:Number; 

     public function Hexagon() 
     { 
      var myTimer:Timer = new Timer(2000); 
      myTimer.addEventListener(TimerEvent.TIMER, timerFunction); 
      myTimer.start(); 

      startWidth = this.width; 
      startHeight = this.height; 

      trace("startWidth:" + " " + startWidth); 
      trace("startHeight:" + " " + startHeight); 
     } 

     function timerFunction (evt:TimerEvent):void 
     { 

     } 
    } 
} 
+0

的問題是你不能使用Silverlight。 – 2010-02-13 19:43:06

+0

你的課程延伸到什麼階層? 也許你可以發佈它的簡化版本,沒有它很難說出什麼問題。 – 2010-02-13 19:46:04

+0

擴展MovieClip。我已經建議我的原始問題包括簡化代碼。 – cmal 2010-02-13 19:49:18

回答

0

是的,這是標準的,那是因爲你要求的寬度和高度的構造,在這一點沒有確定財產/訪問設置。等到屬性被正式設置後,在Event.ADDED_TO_STAGE後最可靠。這是爲我工作:

package 
{ 
    import flash.display.*; 
    import flash.events.*; 
    import flash.utils.*; 

    public class Movie extends MovieClip 
    { 
     public var startWidth:Number; 
     public var startHeight:Number; 

     public function Movie() 
     { 
      var myTimer:Timer = new Timer(2000); 
      myTimer.addEventListener(TimerEvent.TIMER, timerFunction); 
      myTimer.start(); 

      startWidth = this.width; 
      startHeight = this.height; 

      trace("startWidth:" + " " + startWidth); 
      trace("startHeight:" + " " + startHeight); 
      addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler); 
     } 

     public function addedToStageHandler(event:Event):void 
     { 
      startWidth = this.width; 
      startHeight = this.height; 

      trace("new startWidth:" + " " + startWidth); 
      trace("new startHeight:" + " " + startHeight); 
     } 

     public function timerFunction(evt:TimerEvent):void 
     { 

     } 
    } 
} 

讓我知道是否有幫助, 蘭斯