【问题标题】:Flex 4 disptaching custom event from custom component (why flex converting custom event to mouseevent)Flex 4 从自定义组件调度自定义事件(为什么 flex 将自定义事件转换为 mouseevent)
【发布时间】:2012-03-20 15:59:26
【问题描述】:

这不是我的earlier post 的重复(略有不同)

但这是类似的问题,错误相似,但错误不同

从我的自定义组件调度自定义事件时,我现在遇到的错误如下

TypeError:错误 #1034:类型强制失败:无法将 events::MapEvent@a74ab51 转换为 flash.events.MouseEvent。

     dispatchEvent(new MapEvent(MapEvent.CLICKED_ON_MAP));

注意:我的earlier post 中的错误给出以下错误消息

类型强制失败:无法将 flash.events::Event@81ecb79 转换为 com.events.ShopEvent

这里的区别有两件事,早期的错误是在将 flash 事件转换为自定义事件时,现在这个是在将自定义事件转换为 flash 事件时,其次,我不知道为什么它试图转换为 mouseevent我只是用适当的侦听器调度我的自定义事件。

有人可以纠正我在这里做错了什么吗?

这是我的客户活动

package events
{
    import flash.events.Event;

    import ui.map.MapElement;

    public class MapEvent extends Event
    {
        public static const NEW_ELEMENT_ATTACHED:String = "newElementAttached";
        public static const CLICKED_ON_MAP:String = "clickedOnMap";
        public static const CLICKED_ON_ELEMENT:String = "clickedOnElement";

        public var element:MapElement;

        public function MapEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false){
            super(type, bubbles, cancelable);
        }

        override public function clone():Event{

            var c:MapEvent = new MapEvent(type, bubbles, cancelable);
            c.element = this.element;
            return c;
        }
    }
}

这就是我从自定义组件(WorldMap.as 类文件)调度事件的方式

    private function clickHandler(e:MouseEvent):void{
        e.stopImmediatePropagation();
        trace("worldmap click handler");
        if (dragInProgress){
            /*trace ("stopping event propagation");*/
            dragInProgress = false;
            return;
        }else{
            trace("dispatching proxy click event");
            dispatchEvent(new MapEvent(MapEvent.CLICKED_ON_MAP));
        }
    }

我也声明了 [Event] 元标记

[Event(name="newElementAttached", type="events.MapEvent")]
[Event(name="clickedOnMap", type="events.MapEvent")]
[Event(name="clickedOnElement", type="events.MapEvent")]

最后监听器被附加到其他组件(controller.as)

        _userWorld.addEventListener(MapEvent.CLICKED_ON_MAP,clickedOnWorldMap);
        _userWorld.addEventListener(MapEvent.CLICKED_ON_ELEMENT,clickedOnElement);

    private function clickedOnWorldMap(e:MouseEvent):void{
        _draggingMapElement.hideBaseGrid();
        _draggingMapElement = null;
    }
    private function clickedOnElement(e:MapEvent):void{

    }

【问题讨论】:

  • 为什么要投反对票?如果我纠正自己,我不应该发布答案吗?
  • 是否对某些成员的热情投了反对票?

标签: actionscript-3 apache-flex events actionscript flex4


【解决方案1】:

这是我的错。我修复了这个愚蠢的错误。

我更改了列表函数定义。 (将mouseevent替换为MapEvent)

    private function clickedOnWorldMap(e:MapEvent):void{
        _draggingMapElement.hideBaseGrid();
        _draggingMapElement = null;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-12
    • 2010-12-01
    • 2011-02-23
    • 1970-01-01
    • 2012-02-20
    • 2010-10-23
    • 2011-12-22
    • 1970-01-01
    相关资源
    最近更新 更多