0

此问题已停止开发,我无法弄清楚出了什么问题。我不清楚 flex 是如何连接这两个不相关的事件的。Some1 Sprite 在 MouseEvent.MOUSE_UP 上调度 Some1Event.MOUSE_UP 事件,同时停止 MouseEvent 的传播。Some1, Some2 的父级在 Some2 上停止 MouseEvent.MOUSE_UP 时类似地调度 Some2Event.MOUSE_UP。强制错误在 Some2Event 和 Some1Event 之间。有了这个错误,我尝试了其他 #1034 解决方案:

  1. 在主应用程序中添加 Some1Event 和 Some2Event 的实例化。
  2. 用“新事件”包装 - 我无法将事件作为事件类型分派,因为它具有自定义属性。

我还尝试了以下代码:

    import flash.utils.getQualifiedClassName;
    private var systemManager:SystemManager=new SystemManager;

    private function addEventTracer():void
    {
        systemManager.addEventListener(Some1Event.MOUSE_UP, eventTracer, true);
    }

    private function eventTracer(event:Event):void
    {
        trace("Got ", event.type, "from", event.target, "as",
            getQualifiedClassName(event));
    }

但它没有报告任何内容。

在调度事件 Some2Event 时,我收到错误

TypeError: Error #1034: Type Coercion failed: cannot convert  ::Some2Event@117c6c831 to Some1Event.
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at ::Some2/mouseUpHandler()[Some2.as:154]

代码结构为 Some2 (Sprite) 有许多 Some1 (Sprite) 的子代。在 Some2 中,我在 Some1 Sprite 上侦听 MouseEvent.MOUSE_UP 事件,在该侦听器中,停止传播并调度自定义 Some2Event:

    private function mouseUpHandler(event:MouseEvent):void{
        event.stopImmediatePropagation();
        var ev:Some2Event=new Some2Event(Some2Event.MOUSE_UP);
        ev.node2=this;
        dispatchEvent(ev);

在 Some2 中,我有以下注册的事件侦听器:

    private function registerListeners():void
    {
        addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler); //detects mouse up on Some2 Sprite
        addEventListener(Some1Event.MOUSE_UP, some1MouseUpHandler); //detects mouse up on Some1 Sprite, custom event, Some1 mouse up stopped in Some1
        addEventListener(Event.ADDED, addedHandler); //detecting add of child Some1
    }

Some1Event 和 Some2Event 几乎相同:

import flash.display.Sprite;
import flash.events.Event;


public class Some1Event extends Event
{

    public static const MOUSE_DOWN:String = "MOUSE_DOWN";
    public static const MOUSE_UP:String = "MOUSE_UP";

    public var node:Sprite;

    public function Some1Event(type:String, bubbles:Boolean=true, cancelable:Boolean=false)
    {
        super(type, bubbles, cancelable);
    }
    public override function clone():Event
    {
        return new Some1Event(type, bubbles, cancelable);
    }

}

和 Some2Event:

import flash.display.Sprite;
import flash.events.Event;

public class Some2Event extends Event
{
    public static const MOUSE_DOWN:String = "MOUSE_DOWN";
    public static const MOUSE_UP:String = "MOUSE_UP";

    public var node2:Sprite;

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

    public override function clone():Event
    {
        return new Some2Event(type, bubbles, cancelable);
    }

}

任何帮助绝对值得赞赏。

4

1 回答 1

1

Some1Event 中的常量:

public static const MOUSE_DOWN:String = "MOUSE_DOWN";
public static const MOUSE_UP:String = "MOUSE_UP";

Some2Event 中的常量:

public static const MOUSE_DOWN:String = "MOUSE_DOWN";
public static const MOUSE_UP:String = "MOUSE_UP";

在每种情况下,常数值都是相同的。'MOUSE_DOWN' 或 'MOUSE_UP' 。您希望 Flex 如何区分两者?不能!将常量值更改为唯一

在 Some1Event 中:

public static const MOUSE_DOWN:String = "MOUSE_DOWN_SOME1";
public static const MOUSE_UP:String = "MOUSE_UP_SOME1";

在 Some2Event 中:

public static const MOUSE_DOWN:String = "MOUSE_DOWN_SOME2";
public static const MOUSE_UP:String = "MOUSE_UP_SOME2";

现在,Flex 会将这两个事件视为不同/唯一的,并且在分派另一个事件时不会为一个事件调用侦听器。

于 2012-07-16T13:54:23.523 回答