在自定义事件覆盖的clone()为AS3 ...需要帮助 [英] Override Clone() in custom event for AS3... need help

查看:179
本文介绍了在自定义事件覆盖的clone()为AS3 ...需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了这是pretty的基本自定义连类。但调用的事件,然后转发该事件到另一个类我也遇到了无法将thisEvent为thisOtherEvent错误时。

我知道这是因为我需要重写Clone功能在我的自定义事件像这样:

 包COM
{

 进口flash.disply *。
 进口对象类型:flash.events.Event;

 公共类MyCustomEvents扩展事件
 {

  公共静态常量SOME_EVENT:字符串=some_event;
  公共变种信息:对象;

  公共职能MyCustomEvents($类型:字符串,$信息:对象,$气泡:布尔=假,$取消:布尔= FALSE)
  {

   超($类型,$气泡,$取消);
   this.info = $信息;

  }

  公众覆盖功能的clone():事件{
   返回新MyCustomEvents($类型,$气泡,$取消);
  }

 }
}
 

不过我仍然得到这个错误,当我发出事件。别的我可能会丢失?

下面是错误: 类型错误:错误#1034:类型强制失败:不能转换com.greensock.events::TransformEvent@d8df709到com.customEvents.MyCustomEvents

我试图铸造的情况下,在code像这样:

  VAR deleteImgEvent:MyCustomEvent =新MyCustomEvent(MyCustomEvents.IMAGE_DELETE,{imgData:GETIMG},真,假); this.dispatchEvent(deleteImgEvent为MyCustomEvents);
 

仍然没有运气。

更新:

好了,好像问题出在greensock变换库。当事件处理程序,我自定义的事件被称为,我运行TransformManager类的功能。

  _manager.deleteSelection();
 

在那个阶级它派出TransformEvent。不知道为什么,但读了删除事件作为MyCustomEvent。

解决方案

  / **
 * @用法
 * VAR myEvent:自定义事件=新的自定义事件(CustomEvent.EVENT_TYPE_A,{integerRelatedToEvent:5,stringRelatedToEvent:'易'});
 *的addEventListener(CustomEvent.EVENT_TYPE_A,traceCustomEvent);
 *讯(myEvent);
 *功能traceCustomEvent($ E:自定义事件):无效{
 *跟踪($ e.type);
 *}
 * /
包 {
    进口对象类型:flash.events.Event;

    公共类自定义事件扩展事件{
        //类型:
        公共静态常量EVENT_TYPE_A:字符串='CustomEvent.EVENT_TYPE_A';
        公共静态常量EVENT_TYPE_B:字符串='CustomEvent.EVENT_TYPE_B';
        //组件:
        私人VAR _customDatum:对象;
        公共职能得到customDatum():对象{返回_customDatum; }


        公共功能自定义事件($类型:字符串,$ customDatum:对象){
            超($类型);
            _customDatum = $ customDatum;
        }

        公众覆盖功能的clone():事件{
            返回新的自定义事件(类型,_customDatum);
        }
    }
}
 

  

当创建自己的自定义事件   类时,必须覆盖继承   为了的Event.clone()方法,它   为复制的属性您   定制类。如果您没有设置所有   您在添加的属性您   事件子类,这些属性将   没有正确的价值观时,   监听器处理重新调度   事件。

I have created a custom even class which is pretty basic. But when calling an event and then relaying that event to another class I have encountered the "cannot transform thisEvent into thisOtherEvent" error.

I realize this is because I needed to override the Clone function in my custom event like so:

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

 }
}

However I am still getting this error when I dispatch the event. Anything else I might be missing?

here is the error: TypeError: Error #1034: Type Coercion failed: cannot convert com.greensock.events::TransformEvent@d8df709 to com.customEvents.MyCustomEvents.

I tried casting the event in the code like so:

var deleteImgEvent:MyCustomEvent = new MyCustomEvent(MyCustomEvents.IMAGE_DELETE, {imgData: getImg}, true, false); this.dispatchEvent(deleteImgEvent as MyCustomEvents);

Still no luck.

UPDATE:

Ok, seems like the problem is in the greensock Transform library. When the event handler for my custom event is called, I run a function of the TransformManager class.

_manager.deleteSelection();

Inside that class it dispatched a TransformEvent. Not sure why, but it is reading that delete event as a MyCustomEvent.

解决方案

/**
 * @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);
        }
    }
}

"When creating your own custom Event class, you must override the inherited Event.clone() method in order for it to duplicate the properties of your custom class. If you do not set all the properties that you add in your event subclass, those properties will not have the correct values when listeners handle the redispatched event."

这篇关于在自定义事件覆盖的clone()为AS3 ...需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆