Subclassing Object Caveat
29-Jun-05
Ran across an interesting little 'quirk' when subclassing Object in Flash, in that the scope of the instance variables isn't always respected.
Take the following class, for example:
-
class Tester
-
extends Object
-
{
-
private var _Width:Number;
-
private var _Height:Number;
-
private var _Rotation:Number;
-
public function get Width():Number { return _Width; }
-
public function set Width( value:Number ) { _Width = value; }
-
public function get Height():Number { return _Height; }
-
public function set Height( value:Number ) { _Height = value; }
-
public function get Rotation():Number { return _Rotation; }
-
public function set Rotation( value:Number ) { _Rotation = value; }
-
public function Tester()
-
{
-
_Width = 100;
-
_Height = 100;
-
}
-
}
Then place this on the first frame of your flash movie.
-
var t:Tester = new Tester();
-
-
trace( _root._Width );
-
trace( _root._wIdTh );
-
trace( _root._ROTATION );
-
_root._rOtAtIoN = 50;
-
-
t.Width = 50;
-
trace( t.Rotation );
You'll notice that any properties set on the Tester instance affect the _root, and vice versa (the last trace of t.Rotation, which should trace 'undefined', as the _Rotation property of the instance was never initialized).
Now set the superclass of the Tester class to MovieClip instead of Object. Republish. Boom. Everything is good.
One other thing you'll notice from the trace output is that case sensitivity is not respected on _root. Yes, I am publishing for Flash Player 7 and AS 2.0, so case-sensitivity should, by all accounts, be in effect. But the player is just as happy tracing _root._WiDtH as _root._width.
I can't tell you how much of a headache this just caused me. Probably about 3-4 trying to figure out why the scaling of my app was completely wonky.
Long story short: beware of scope collisions when subclassing Object. Your _root ain't sacred.




