Oleg Sivokon
2010-03-10 10:28:10 UTC
Once I encountered this problem myself, I was told that in this case I would
have to prefer the composition over inheritance :) And if you think about
for a while - it makes sense. DisplayObjects are big and robust classes
relatively to what you will add to them -> when you override something there
you may break some internal mechanism without even knowing that. Take for
example width property - it will also change when you modify scaleX,
transform.matrix, draw something into graphics (if this display object has
it), or, if it is a TextField with autoSize other then
TextFieldAutosize.NONE, then the width may change as you add / remove text
from it, change the scrollRect, change rotation (now, if you rotate by odd
number * 90 you swap height and width, besides, the width is always measured
along the parent X axis, so, every degree of rotation will produce a
different width value). This all means that in order to get a "widthChanged"
sort of event you must override a lot of properties, where some of them are
not really under your control because, for example Graphics is a final
class, and, I think that Transform is also a final class, besides, you can
also change size of the display object by passing it to a new instance of
Transform class and applying transformation there (AFAIK, this kind of
transformation bypasses width setter).
So, IMO, your best bet is to create a MyDisplayObject class with the only
property called width (or many similar properties, if you fill like it :))
and then apply the transformation to the display object contained within
this class.
Best.
Oleg
have to prefer the composition over inheritance :) And if you think about
for a while - it makes sense. DisplayObjects are big and robust classes
relatively to what you will add to them -> when you override something there
you may break some internal mechanism without even knowing that. Take for
example width property - it will also change when you modify scaleX,
transform.matrix, draw something into graphics (if this display object has
it), or, if it is a TextField with autoSize other then
TextFieldAutosize.NONE, then the width may change as you add / remove text
from it, change the scrollRect, change rotation (now, if you rotate by odd
number * 90 you swap height and width, besides, the width is always measured
along the parent X axis, so, every degree of rotation will produce a
different width value). This all means that in order to get a "widthChanged"
sort of event you must override a lot of properties, where some of them are
not really under your control because, for example Graphics is a final
class, and, I think that Transform is also a final class, besides, you can
also change size of the display object by passing it to a new instance of
Transform class and applying transformation there (AFAIK, this kind of
transformation bypasses width setter).
So, IMO, your best bet is to create a MyDisplayObject class with the only
property called width (or many similar properties, if you fill like it :))
and then apply the transformation to the display object contained within
this class.
Best.
Oleg