在未出现在舞台上的类上调度带有气泡的事件
我有小型 swf as3 flash 应用程序,由主 Flex 应用程序加载。
主 Flex 应用程序包含一个 SWFLoader,我添加了一个事件侦听器 SWFLoader.content 来获取我准备的自定义事件。
现在,每当我在应用程序中使用函数 dispatchEvent
来创建事件时,我都会确保事件的 bubbles 参数为 true。因此在这种情况下,事件会在舞台中冒泡,直到到达我的事件侦听器并且我可以正确处理该事件。
当我有一个静态类并且我想从那里分派事件时,问题就出现了。因为它是一个静态类,而不是出现在我的舞台上的显示对象,所以它没有地方可以冒泡,因此,即使在我的主 Flex 应用程序中,我也无法获取自定义内容。
我尝试使用 eventDispatcher 和以下代码:
new EventDispatcher().dispatchEvent(new CustomEvent(CustomEvent.EVENT_setAttribute,{'attr_name':attrName,'attr_val':attrVal)));
我的自定义事件的构造函数获取参数作为第二个参数。
所以看来,当我使用事件调度程序时,事件仍然没有地方冒泡,这就是为什么我在主应用程序中也没有收到它。
我该如何解决这个问题?我需要能够在舞台上未出现的对象上正确调度事件。
main 我需要为此函数提供一个出现在舞台上的对象,以便使用它来调度事件。我确实希望有更好的解决方案来解决这个问题。
任何信息将不胜感激。
这是我的自定义事件类:
public class CustomEvent extends Event
{
public var command:String;
public var params:Object;
public static const CONTROL_TYPE:String = "eMyControl";
private static var EVENT_setAttribute:String = "set_attribute";
private static var gameAttributes:Object = new Object();
public static function setAttribute(attrName:String,attrVal:String):void {
gameAttributes[attrName]=attrVal;
new EventDispatcher().(new XpoEvent(XpoEvent.EVENT_setAttribute,{'attr_name':attrName,'attr_val':attrVal}));
}
public function CustomEvent(eventName:String, params:Object = null, bubbles:Boolean = true, cancelable:Boolean = false)
{
var _loc_6:String = null;
var _loc_5:String = "";
if (params != null)
{
_loc_5 = "params: ";
for (_loc_6 in params)
{
_loc_5 = _loc_5 + (_loc_6 + "=" + params[_loc_6] + " ");
}
}
trace("CustomEvent issued - " + eventName + " " + _loc_5);
super(CONTROL_TYPE, bubbles, cancelable);
this.command = eventName;
if (params != null)
{
this.params = params;
}
return;
}
}
正如您在此处看到的,我有一个名为 setAttribute 的静态函数,它将 attrName 和 Val 传递到数组,然后调度自定义事件。默认情况下,自定义类事件启用了气泡。 看起来仍然是因为我没有使用出现在舞台中的对象来调度事件,所以主 Flex 应用程序无法正确捕获事件。
以下代码确实有效:
<object In Stage>.dispatchEvent(new CustomEvent("testme"));
谢谢
I have small swf as3 flash applications that are loaded by a main flex application.
The main Flex Application contains an SWFLoader and i added an event listener on
SWFLoader.content to fetch custom events that I prepared.
now whenever I like in the application I use the function dispatchEvent
to create the event and I make sure that the bubbles parameter of the event is true. so in this case the event bubbles up in the stage until it reaches my event listener and I can handle the event properly.
the problem resides when I have a static class and I want to dispatch the event from there. because it's a static class and not a display object that appears on my stage it has no where to bubble to and because of that I cannot fetch the custom even in my main flex application.
I tried using eventDispatcher with the following code:
new EventDispatcher().dispatchEvent(new CustomEvent(CustomEvent.EVENT_setAttribute,{'attr_name':attrName,'attr_val':attrVal)));
The constructor of my custom event gets the parameters as the 2nd parameter.
so it seems that when I'm using event dispatcher, the event still has no where to bubble to and that is why I do not receive it in my main application as well.
how can I resolve the issue ? I need to be able to properly dispatch an event on objects that do not appear in the stage.
main I need to provide an object that appear on the stage for this function in order to use it to dispatch the event. I do hope that there is a better solution to resolve this issue.
any information would be greatly appreciated.
this is my custom event class:
public class CustomEvent extends Event
{
public var command:String;
public var params:Object;
public static const CONTROL_TYPE:String = "eMyControl";
private static var EVENT_setAttribute:String = "set_attribute";
private static var gameAttributes:Object = new Object();
public static function setAttribute(attrName:String,attrVal:String):void {
gameAttributes[attrName]=attrVal;
new EventDispatcher().(new XpoEvent(XpoEvent.EVENT_setAttribute,{'attr_name':attrName,'attr_val':attrVal}));
}
public function CustomEvent(eventName:String, params:Object = null, bubbles:Boolean = true, cancelable:Boolean = false)
{
var _loc_6:String = null;
var _loc_5:String = "";
if (params != null)
{
_loc_5 = "params: ";
for (_loc_6 in params)
{
_loc_5 = _loc_5 + (_loc_6 + "=" + params[_loc_6] + " ");
}
}
trace("CustomEvent issued - " + eventName + " " + _loc_5);
super(CONTROL_TYPE, bubbles, cancelable);
this.command = eventName;
if (params != null)
{
this.params = params;
}
return;
}
}
as you can see here I have a static function called setAttribute that passes attrName and Val to an array and then dispatches a custom event. by default the custom class event have bubbles enabled.
it still seems that because I did not dispatch the event using an object that appears in the stage disallows from the main flex application to properly catch the event.
the following code does work:
<object In Stage>.dispatchEvent(new CustomEvent("testme"));
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您创建 EventDispatcher 实例并分派其中的某些内容时,没有人会收到您的事件,因为没有人可以订阅它。
您必须从显示列表中的内容调度事件。但是您的静态类对显示列表一无所知,也无法访问它。您必须在初始化期间向其提供显示列表中的对象。为此,在加载的 SWF 中,您可以传递 someDisplayObject.root,其中 someDisplayObject 位于舞台上。不是 someDisplayObject.stage,因为它将指向容器 Stage,而且可能无法访问。
When you create an EventDispatcher instance and dispatch something of it nobody will get your event because nobody can subscribe for it.
You have to dispatch an event from something which IS in display list. But your static class doesn't know anything about display list niether it can get access to it. You must provide an object in display list to it during initialization. For this purpose in loaded SWF you could pass someDisplayObject.root where someDisplayObject is on stage. Not someDisplayObject.stage because it will point to container Stage and what's more might be inaccessible.