views:

1260

answers:

2

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 the changes made in the Sprite? I guess i need to fire a PropertychangeEvent some how, but I can't see that 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;  
  }  
}
+1  A: 

A quick fix is to override the alpha setter and using the value passed in for the TextField as well.

public override function set alpha(value:Number):void {
    super.alpha = value;
    textField.alpha = value;
}
Johan Öbrink
+6  A: 

One way would be to create a derived class of the sprite 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 simply be to set the textfield alpha whenever the alpha setter of the parent sprite is hit, as shown above just without the event.

Andrew

REA_ANDREW