I've done a lot of reading through forum posts and tutorials, but I still can't wrap my brain round events and event listeners. I have a pretty simple example, but I can't get it to work.
我已经通过论坛帖子和教程做了很多阅读,但我仍然无法将我的大脑包围在事件和事件监听器中。我有一个非常简单的例子,但我无法让它工作。
I have an arrayCollection of custom objects in a repeater, when one of those objects is clicked, I want a different componenet to display data associated with that object.
我在转发器中有一个自定义对象的arrayCollection,当单击其中一个对象时,我想要一个不同的组件来显示与该对象关联的数据。
Here's what I have, but the listener never responds (the dispatcher seems to be working though, because the new event is created and I can see the trace with the proper output.) I suspect it is because when I call addEvent Listener, I am doing so on the wrong object. My understanding is that the object that will display the object data is the object that should have the event listener, and listen for all events of this nature, but maybe I misunderstood.
这就是我所拥有的,但是监听器从不响应(调度程序似乎正在工作,因为新事件已创建,我可以看到具有正确输出的跟踪。)我怀疑这是因为当我调用addEvent Listener时,我是在错误的对象上这样做。我的理解是,显示对象数据的对象是应该具有事件监听器的对象,并且监听这种性质的所有事件,但也许我误解了。
My custom event:
我的自定义事件:
public class SelectObjectEvent extends Event
{
public function SelectObjectEvent(customEventString:String, myObject:customObject)
{
super(customEventString, true, false);
trace(customEventString+" "+myObject);
}
}
}
My custom object has the following function which is called on click:
我的自定义对象具有以下函数,该函数在单击时调用:
public function selectObject(myObject:customObject):void
{
dispatchEvent(new SelectObjectEvent("OBJECT_SELECTED", customObject));
}
And the component I want to display the selected object has the following constructor:
我想要显示所选对象的组件具有以下构造函数:
public function SelectedObjectDisplayClass()
{
addEventListener("OBJECT_SELECTED", this.showObject)
}
public function showObject(event:Event):void
{
trace("Show object: "+event);
}
2 个解决方案
#1
It's not quite clear where your last two code chunks are, but it looks like you need to be calling addEventListener
on the object that extends EventDispatcher
.
目前还不是很清楚你最后两个代码块的位置,但看起来你需要在扩展EventDispatcher的对象上调用addEventListener。
That is, if your second chunk belongs to a custom object called Clickable
, which extends EventDispatcher
and calls dispatchEvent()
when clicked, then your component should be calling myClickable.addEventListener(...)
where myClickable is an instance of Clickable. Does that make sense?
也就是说,如果您的第二个块属于一个名为Clickable的自定义对象,它扩展了EventDispatcher并在单击时调用dispatchEvent(),那么您的组件应该调用myClickable.addEventListener(...),其中myClickable是Clickable的一个实例。那有意义吗?
But assuming your 3rd code chunk is not in the same class as the second, it doesn't look like you're doing that. You're adding a listener to the class that owns the third chunk of code, which I gather is not the one that extends EventDispatcher
.
但是假设您的第3个代码块与第二个代码块不在同一个类中,它看起来就像您正在那样做。您正在为拥有第三块代码的类添加一个侦听器,我收集的不是扩展EventDispatcher的类。
#2
Just a quick glance at your code and notice that your dispatchEvent second parameter is the class not the instance of the object. Shouldn't this be myObject?
只需快速浏览一下代码,注意您的dispatchEvent第二个参数是该类而不是该对象的实例。这不应该是myObject吗?
public function selectObject(myObject:customObject):void
{
dispatchEvent(new SelectObjectEvent("OBJECT_SELECTED", **customObject**));
}
#1
It's not quite clear where your last two code chunks are, but it looks like you need to be calling addEventListener
on the object that extends EventDispatcher
.
目前还不是很清楚你最后两个代码块的位置,但看起来你需要在扩展EventDispatcher的对象上调用addEventListener。
That is, if your second chunk belongs to a custom object called Clickable
, which extends EventDispatcher
and calls dispatchEvent()
when clicked, then your component should be calling myClickable.addEventListener(...)
where myClickable is an instance of Clickable. Does that make sense?
也就是说,如果您的第二个块属于一个名为Clickable的自定义对象,它扩展了EventDispatcher并在单击时调用dispatchEvent(),那么您的组件应该调用myClickable.addEventListener(...),其中myClickable是Clickable的一个实例。那有意义吗?
But assuming your 3rd code chunk is not in the same class as the second, it doesn't look like you're doing that. You're adding a listener to the class that owns the third chunk of code, which I gather is not the one that extends EventDispatcher
.
但是假设您的第3个代码块与第二个代码块不在同一个类中,它看起来就像您正在那样做。您正在为拥有第三块代码的类添加一个侦听器,我收集的不是扩展EventDispatcher的类。
#2
Just a quick glance at your code and notice that your dispatchEvent second parameter is the class not the instance of the object. Shouldn't this be myObject?
只需快速浏览一下代码,注意您的dispatchEvent第二个参数是该类而不是该对象的实例。这不应该是myObject吗?
public function selectObject(myObject:customObject):void
{
dispatchEvent(new SelectObjectEvent("OBJECT_SELECTED", **customObject**));
}