我创建了一个非常基本的自定义偶数类。但是当调用一个事件然后将该事件转发给另一个类时,我遇到了“无法将thisEvent转换为thisOtherEvent”错误。
我意识到这是因为我需要在我的自定义事件中覆盖克隆功能,如下所示:
package com
{
import flash.disply.*;
import flash.events.Event;
public class MyCustomEvents extends Event
{
public static const SOME_EVENT:String = "some_event";
public var info:Object;
public function MyCustomEvents($type:String, $info:Object,$bubbles:Boolean = false, $cancelable:Boolean = false)
{
super($type, $bubbles, $cancelable);
this.info = $info;
}
public override function clone():Event {
return new MyCustomEvents($type, $bubbles, $cancelable);
}
}
}
但是,当我发送事件时,我仍然收到此错误。还有什么我可能会遗失的吗?
这是错误:
TypeError: Error #1034: Type Coercion failed: cannot convert com.greensock.events::TransformEvent@d8df709 to com.customEvents.MyCustomEvents.
我尝试在代码中强制转换事件:
var deleteImgEvent:MyCustomEvent = new MyCustomEvent(MyCustomEvents.IMAGE_DELETE, {imgData: getImg}, true, false); this.dispatchEvent(deleteImgEvent as MyCustomEvents);
仍然没有运气。
更新:
好的,似乎问题出在greensock Transform库中。当我调用自定义事件的事件处理程序时,我运行了一个TransformManager类的函数。
_manager.deleteSelection();
在该类中,它调度了一个TransformEvent。不知道为什么,但它正在读取删除事件作为MyCustomEvent。
答案 0 :(得分:2)
/**
* @usage
* var myEvent:CustomEvent = new CustomEvent(CustomEvent.EVENT_TYPE_A, { integerRelatedToEvent: 5, stringRelatedToEvent: 'easy' });
* addEventListener(CustomEvent.EVENT_TYPE_A, traceCustomEvent);
* dispatch(myEvent);
* function traceCustomEvent ($e:CustomEvent):void {
* trace($e.type);
* }
*/
package {
import flash.events.Event;
public class CustomEvent extends Event {
// Types:
public static const EVENT_TYPE_A:String = 'CustomEvent.EVENT_TYPE_A';
public static const EVENT_TYPE_B:String = 'CustomEvent.EVENT_TYPE_B';
// Components:
private var _customDatum:Object;
public function get customDatum ():Object { return _customDatum; }
public function CustomEvent ($type:String, $customDatum:Object) {
super($type);
_customDatum = $customDatum;
}
public override function clone ():Event {
return new CustomEvent(type, _customDatum);
}
}
}
“创建自己的自定义事件时 class,你必须覆盖继承 Event.clone()方法为了它 复制你的属性 自定义类。如果你没有全部设置 您添加的属性 事件子类,那些属性将 没有正确的值时 侦听器处理redispatched 事件“。
答案 1 :(得分:1)
package com.events;
{
import flash.events.Event;
public class XMLLoaderEvent extends Event
{
public static const XML_LOADED:String = "XML_Loaded";
public var data:*;
public var properties:Object;
public function XMLLoaderEvent( type:String,_data:*,bubbles:Boolean = false,cancelable:Boolean = false):void
{
super( type, bubbles, cancelable );
data = _data;
}
// Override clone
override public function clone():Event
{
return new XMLLoaderEvent( type, data, bubbles, cancelable);
}
}
}
答案 2 :(得分:0)
不知道是不是这样,但你的自定义事件中有一个额外的参数$info:Object
,但你没有在克隆构造函数中传递它。
return new MyCustomEvents(type, info, bubbles, cancelable);
答案 3 :(得分:-1)
我认为您需要clone函数来返回MyCustomEvents类型。不是事件类型。您需要添加上一张海报所述的info参数。
package com {
import flash.display.*;
import flash.events.Event;
public class MyCustomEvents extends Event {
public static const SOME_EVENT:String = "some_event";
public var info:Object;
public function MyCustomEvents($type:String, $info:Object,$bubbles:Boolean = false, $cancelable:Boolean = false) {
super($type, $bubbles, $cancelable);
this.info = $info;
}
public override function clone():MyCustomEvents {
return new MyCustomEvents(this.type, this.info, this.bubbles, this.cancelable);
}
}
}