有没有办法监听加载的 SWF 中 flash.display.DisplayObject 可见性的变化?

发布于 2024-08-21 18:21:46 字数 593 浏览 10 评论 0原文

我正在使用 flash.display.Loader 加载外部 SWF(由我工作的公司的其他部门预先制作)。然后,我遍历显示列表,注册所有对象并侦听添加/删除的事件以处理列表的未来更改。

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);

问题是我想知道如果原始 SWF 的开发人员之一使用:

.visible = true / false;

是否有一种方法可以侦听 .visible 属性的更改? 或者任何其他属性(不会触发内置事件)?

I'm loading an external SWF (pre-made by an other department of the company I work for) using flash.display.Loader. I then traverse the display list, register all objects and listen for added/removed 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);

The thing is I wonder what would happen if one of the developers of the original SWFs uses:

.visible = true / false;

Is there a way I can listen for changes to the .visible property?
Or any other property (that doesn't fire a built-in event) for that matter?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

吻泪 2024-08-28 18:21:46

我不认为默认情况下会这样。但是您可以创建自己的 Event 类,并从 function set Visible 的重写中分派它,而不会有很多麻烦。

编辑:
好的,所以 a_w 确实有正确的想法,您可以将其直接附加回容器中。

像这样创建一个包装器:

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;
}

然后,让你的循环看起来像:

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
}

然后应该多态地调用 setter 方法。您可能必须通过 VisibilityEnabledDisplayObject 装饰器传递整个公共 DisplayObject 接口,我现在不记得了......

Not by default, I don't think. But you could create your own Event class and dispatch it from an override of function set Visible without a lot of hassle.

Edit:
Okay, so a_w does have the right idea, and you can attach that right back into the container.

Make a wrapper like so:

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 should be polymorphically called. You may have to pass the entire public DisplayObject interface through the VisibilityEnabledDisplayObject decorator, I can't remember right now...

放低过去 2024-08-28 18:21:46

对于 DisplayObject,您希望侦听 addedToStageremovedFromStage 事件。

对于 Flex 组件,监听 showhide 事件。

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

For Flex components, listen to show and hide events.

听,心雨的声音 2024-08-28 18:21:46

请参阅 Actionscript 组件可以侦听其自己的 propertyChange 事件吗?

它说是的。

如果使用 [Bindable] 标记而不指定事件类型,则当属性更改其值时,将调度 PropertyChangeEvent.PROPERTY_CHANGE 类型的事件,即字符串“propertyChange”。

因此,为了能够注册监听该事件,您需要说:

this.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE, onOnChange);

您的监听器函数从未被调用的原因是事件类型不正确。

请注意,当类中标记为 Bindable 的任何变量发生更改(而不仅仅是“on”)时,将调用侦听器方法。该事件附带一个名为“property”的属性,指示哪个变量已更改。

为了避免在每个 Bindable 变量上被调用,您需要在 [Bindable] 标记中指定一个事件:

[Bindable(event="myOnChangeEvent")]

并在您认为属性正在更改时手动分派该事件(即:在 setter 中),尽管这似乎并没有成为你想做的事。

See Can an Actionscript component listen to its own propertyChange events?

It says yes.

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:

this.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE, onOnChange);

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:

[Bindable(event="myOnChangeEvent")]

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.

薄荷梦 2024-08-28 18:21:46

创建一个中介类,它将操纵您的显示对象。通过该类的实例,您可以设置和获取任何属性并监视更改。例如

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'));
      }
   }
}

Create a mediator class, that will manipulate your display object. through instance of that class you can set and get any properties and monitor changes. for example

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'));
      }
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文