AS3: How can I fire events when a property is changed in the über class?
I have a TextField inside a Sprite and I always want the alpha of the TextField to be equal to the alpha of the sprite. How can I subscribe to changes made to Sprite? I think I need to trigger the PropertychangeEvent somehow, but I can't see the sprite supports this out of the box?
class TextWidget extends Sprite{
private var textfield:TextField;
public function TextWidget(){
textfield = new TextField();
textfield.alpha = this.alpha; //does'n help
addChild(textField);
??
this.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE, updateAlpha);
??
}
private function updateAlpha(event:PropertychangeEvent):void{
textfield.alpha = this.alpha;
}
}
0
Vegard
source
to share
2 answers
One way is to derive the sprite class and override the alpha property
/**
* ...
* @author Andrew Rea
*/
public class CustomSprite extends Sprite
{
public static const ALPHA_CHANGED:String = "ALPHA_CHANGED";
public function CustomSprite()
{
}
override public function get alpha():Number { return super.alpha; }
override public function set alpha(value:Number):void
{
super.alpha = value;
dispatchEvent(new Event(CustomSprite.ALPHA_CHANGED));
}
}
Another way would be to just set the alpha textarea when the parent sprite's alpha setter is configured as shown above, without an event.
Andrew
+6
source to share