【问题标题】:Type Coercion failing when calling dispatchEvent with custom event and "this"使用自定义事件和“this”调用 dispatchEvent 时类型强制失败
【发布时间】:2013-06-10 20:20:58
【问题描述】:

我收到以下错误

TypeError: Error #1034: Type Coercion failed: cannot convert EmergencyItems::EmergencyEvent@c607bb1 to EmergencyItems.EmergencyItem.
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at EmergencyItems::EmergencyItem/request()[\EmergencyItems\EmergencyItem.as:64]

EmergencyEvent.as

public class EmergencyEvent extends Event {

    public static const EVENT_REQUEST:String = "requestingr";


    public var item:EmergencyItem;

    public function EmergencyEvent(type:String, item:EmergencyItem, bubbles:Boolean=false, cancelable:Boolean=false) {
        super(type, bubbles, cancelable);
        this.item = item;
    }

    override public function clone():Event {
        return new EmergencyEvent(type, item);
    }
}

EmergencyItem.as(第 64 行以“dispatchEvent...”开头)

public class EmergencyItem extends EventDispatcher {      

    public function request():void {
        dispatchEvent(new EmergencyEvent(EmergencyEvent.EVENT_REQUEST, this));
    }
}

我已经环顾了很长时间,但找不到问题(或解决方案)。我认为可能是“this”以某种方式引用了 EmergencyEvent 而不是 EmergencyItem,所以我修改了 EmergencyItem.as

public function request():void {
        var self:EmergencyItem = this;
        var eEvent:EmergencyEvent = new EmergencyEvent(EmergencyEvent.EVENT_REQUEST, self);
        dispatchEvent(eEvent);
    }

我仍然遇到同样的错误。

已解决,我在添加事件监听时使用了错误的函数,复制粘贴错误。

【问题讨论】:

  • 抛出错误的行是什么? EmergencyItem 类的第 64 行是什么?
  • 以dispatchEvent开头的那一行
  • @JackFlapper 这个事件的监听器代码是什么?你确定参数是一个 EmerygencyEvent 吗?
  • @fmodos 这就是问题所在,谢谢
  • @JackFlapper 我会发布一个关于它的答案,所以它会对面临同样问题的其他人有所帮助

标签: actionscript-3 apache-flex flex4


【解决方案1】:

问题出在您的侦听器功能上。参数类型必须是EmergencyEvent 而不是EmergencyItem

【讨论】:

    猜你喜欢
    • 2010-11-27
    • 2011-09-26
    • 1970-01-01
    • 2014-07-09
    • 2011-12-18
    • 1970-01-01
    • 1970-01-01
    • 2017-12-31
    • 1970-01-01
    相关资源
    最近更新 更多