0 actionscript event-handling actionscript-3
我在Sprite中有一个TextField,我总是希望TextField的alpha等于sprite的alpha.我如何订阅Sprite中所做的更改?我想我需要解决一些PropertychangeEvent,但我看不到sprite支持这个开箱即用?
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;
}
}
Run Code Online (Sandbox Code Playgroud)
一种方法是创建精灵的派生类并覆盖alpha属性
/**
* ...
* @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));
}
}
Run Code Online (Sandbox Code Playgroud)
另一种方法是只要在命中父精灵的alpha设置器时设置文本字段alpha,如上所示,只是没有事件.
安德鲁