Is there a way to listen for changes in flash.display.DisplayObject visibility in loaded SWF?

I download an external SWF (prepared by another department of the company I work for) using flash.display.Loader . Then I look at the display list, register all objects and listen to the added / deleted events to handle future changes to the list.

 var obj:DisplayObjectContainer = loadEvent.currentTarget.content as DisplayObjectContainer; var count:Number = obj.numChildren; for (var i:Number = 0; i < count; i++) { registerObject(obj.getChildAt(i)); } obj.addEventListener(Event.ADDED, onChildAdded); obj.addEventListener(Event.REMOVED, onChildRemoved); 

I wonder what will happen if one of the developers of the original SWF uses:

.visible = true / false;

Is there a way to listen for changes to the .visible property? Or any other property (which does not fire an inline event), for that matter?

+4
source share
4 answers

Not the default, I don't think so. But you can create your own Event class and send it from the function set Visible override without much hassle.

Edit: Okay, so a_w has the right idea, and you can attach it back to the container.

Create a wrapper as follows:

 public class VisibilityEnabledDisplayObject extends DisplayObject { public function VisibilityEnabledDisplayObject(d:DisplayObject) { super(); _d = d; } public function override get visible(b:Boolean):Boolean { return this._d.visible; } public function set visible(b:Boolean):void { if(this._d.visible != value) { this._d.visible = value; this.dispatchEvent(new Event('visibilityChanged')); } } private var _d:DisplayObject; } 

Then make your loop look like:

 for (var i:int = 0; i < count; i++) { var do:DisplayObject = obj.getChildAt(i); registerObject(do); obj.removeChildAt(i); var vedo:VisibilityEnabledDisplayObject = new VisibilityEnabledDisplayObject(do); obj.addChildAt(vedo, i); // register whatever listener you like with the vedo here } 

Then the setter method must be polymorphically called. You may need to pass the entire open DisplayObject interface through the VisibilityEnabledDisplayObject decorator, I can't remember right now ...

+3
source

For DisplayObject you want to listen to the addedToStage and removedFromStage events.

For Flex components, listen for show and hide events.

+1
source

See Can an Actionscript component listen for its own events? Have events?

It says yes.

If you use the [Bindable] tag without specifying the type of event, then when the property changes its value, an event of the type: PropertyChangeEvent.PROPERTY_CHANGE, which is the string 'PropertyChange', will be sent.

Therefore, in order to be able to register to listen to this event, you need to say:

 this.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE, onOnChange); 

The reason your listener function has never been called is because the type of event was wrong.

Note that the listener method will be called if any of the variables marked as Bindable in your class change, not just 'on'. This event has a property โ€œpropertyโ€ that indicates which variable has been changed.

To avoid calling each Bindable variable, you need to specify the event in the [Bindable] tag:

 [Bindable(event="myOnChangeEvent")] 

and send this event manually, given that the property is changing (i.e.: in the installer), although this is not like what you wanted to do.

+1
source

Create a mediation class that will manage your display object. through an instance of this class, you can set and receive any properties and track changes. eg

  public class ExistingDOMediator extends EventDispatcher {
    protected var _view: DisplayObject;
    public function ExistingDOMediator (view: DisplayObject): void {
       super ();
       this._view = view;
    }
    public function get visible (value: Boolean): void {
       return this._view.visible;
    }
    public function set visible (value: Boolean): void {
       if (this._view.visible! = value) {
          this._view.visible = value;
          this.dispatchEvent (new Event ('visibilityChanged'));
       }
    }
 }
+1
source

Source: https://habr.com/ru/post/1301491/


All Articles