有没有办法监听加载的 SWF 中 flash.display.DisplayObject 可见性的变化?
我正在使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不认为默认情况下会这样。但是您可以创建自己的 Event 类,并从
function set Visible
的重写中分派它,而不会有很多麻烦。编辑:
好的,所以 a_w 确实有正确的想法,您可以将其直接附加回容器中。
像这样创建一个包装器:
然后,让你的循环看起来像:
然后应该多态地调用 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:
Then, make your loop look like:
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...
对于
DisplayObject
,您希望侦听addedToStage
和removedFromStage
事件。对于 Flex 组件,监听
show
和hide
事件。For
DisplayObject
you want to listen for theaddedToStage
andremovedFromStage
events.For Flex components, listen to
show
andhide
events.请参阅 Actionscript 组件可以侦听其自己的 propertyChange 事件吗?
它说是的。
如果使用 [Bindable] 标记而不指定事件类型,则当属性更改其值时,将调度 PropertyChangeEvent.PROPERTY_CHANGE 类型的事件,即字符串“propertyChange”。
因此,为了能够注册监听该事件,您需要说:
您的监听器函数从未被调用的原因是事件类型不正确。
请注意,当类中标记为 Bindable 的任何变量发生更改(而不仅仅是“on”)时,将调用侦听器方法。该事件附带一个名为“property”的属性,指示哪个变量已更改。
为了避免在每个 Bindable 变量上被调用,您需要在 [Bindable] 标记中指定一个事件:
并在您认为属性正在更改时手动分派该事件(即:在 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:
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.
创建一个中介类,它将操纵您的显示对象。通过该类的实例,您可以设置和获取任何属性并监视更改。例如
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