this.width在一个类中总是返回相同的值

问题描述:

在我的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

是的,这是标准的,那是因为你要求的宽度和高度的构造,在这一点没有确定财产/访问设置。等到属性被正式设置后,在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 
     { 

     } 
    } 
} 

让我知道是否有帮助, 兰斯