vendredi 20 novembre 2015

Why do I get a 0 width for a class instantiated by static variable

Here is a Monitor class. I am trying to get it's width, through it's static property. So that, to get width, I can write : Monitor.width, Monitor.height

However, when I instantiate, it through a static variable, the instantiation is successful, but still returns a 0 value width. Is their any alternative way ( other than instantiating it inside the get width and get height method itself ) ?

package {

import flash.display.*;

public class Monitor extends MovieClip {

    private static var staticInstance: Monitor = new Monitor(); 

    public function Monitor() {

    }
    static public function get width(): Number {
        // staticInstance: Monitor = new Monitor()  <<< this will work
        return staticInstance.width;
    }

    static public function get height(): Number {
       // staticInstance: Monitor = new Monitor()  <<< this will work
        return staticInstance.height;
    }
}

}

When trying :

trace(Monitor.width)  // output is 0 

Aucun commentaire:

Enregistrer un commentaire