I have a CircleButton class in Actionscript. I want to know when someone externally has changed the ‘on’ property. I try listening to ‘onChange’ but it never hits that event handler.
I know I can write the ‘on’ property as a get/setter but I like the simplicity of just using [Bindable]
Can an object not listen to its own events?
public class CircleButton extends UIComponent { [Bindable] public var on:Boolean; public function CircleButton() { this.width = 20; this.height = 20; graphics.beginFill(0xff6600, 1); graphics.drawCircle(width/2, height/2, width/2); graphics.endFill(); this.addEventListener(MouseEvent.ROLL_OVER, rollover); this.addEventListener(MouseEvent.ROLL_OUT, rollout); this.addEventListener('onChange', onOnChange); } private function onOnChange(event:PropertyChangeEvent):void {
If you use the [Bindable] tag without specifying an event type, then when the property changes its value, an event of type: PropertyChangeEvent.PROPERTY_CHANGE, which is the string ‘propertyChange’, will be dispatched.
Therefore, to be able to register to listen to that event, you need to say:
The reason why your listener function was never called is that the event type was not correct.
Note that the listener method will be called when any of the variables marked as Bindable in your class changes, not only ‘on’. This event comes with a property called ‘property’ that indicates which variable was changed.
To avoid being called on each Bindable variable, you need to specify an event in the [Bindable] tag:
and dispatch that event manually when you consider that the property is changing (ie: in the setter), though that didn’t seem to be what you wanted to do.