Search code examples
actionscript-3actionscriptflashheightclass-variables

How to use SWF height for class variable in actionscript3?


I want to use height in [SWF height=200] for class variable.

I tried the following code but it causes compile error.

const SWF_HEIGHT:int = 200;

package {
    import flash.display.*;

    [SWF(backgroundColor=0xffffff, width=400, height=SWF_HEIGHT)]

    public class Main extends Sprite {
        public static const Y:int = SWF_HEIGHT / 2;
    }
}

If I use the number, 200 instead of const SWF_HEIGHT, the compile error doesn't happen.
But I don't want to write the same number on 2 places.

Is there way to avoid writing the same number on 2 places?


Solution

  • -- ANSWER HAS BEEN EDITED --

    You need to do it the other way around:

    package {
        import flash.display.*;
    
        [SWF(backgroundColor=0xffffff, width=400, height=200)]
    
        public class Main extends Sprite {
            private static var _swfHeight : int = -1;
            public static function get swfHeight():int{
                return _swfHeight;
            }
            public function Main():void{
                _loader = this.loaderInfo;
                _loader.addEventListener(Event.COMPLETE, loaded );
            }
            private var _loader : LoaderInfo;
            private function loaded( event : Event ) : void{
                _swfHeight = _loader.height;
            }
        }
    }