类中的this.width始终返回相同的值

时间:2010-02-13 19:36:52

标签: flex flash actionscript-3

在我的AS3类中,我调用this.width,它返回的值始终为1,即使根据对象的内容这是不可能的。

这是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
        {

        }
    }
}

1 个答案:

答案 0 :(得分: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
        {

        }
    }
}

如果有帮助,请告诉我 兰斯