views:

67

answers:

1

Within my AS3 class I am calling this.width, and the value it returns is always 1, even though this is impossible given the contents of the object.

Is this a standard behavior for AS3?

Simple version of class is posted below. It is attached to a MovieClip symbol that just contains a simple hexagon.

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
        {

        }
    }
}
A: 

Yes this is standard, it's because you're asking for the width and height in the Constructor, at which point the property/accessor settings are not established. Wait until the properties have been set officially, which is most reliably after Event.ADDED_TO_STAGE. This is working for me:

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
        {

        }
    }
}

Let me know if that helps, Lance

viatropos