Question

This problem has stopped development, and I can't figure out what's wrong. I'm not clear on how flex is connecting these two unrelated events. The Some1 Sprite dispatches a Some1Event.MOUSE_UP event on a MouseEvent.MOUSE_UP while stopping propagation of the MouseEvent. The parent of Some1, Some2 similarly dispatches a Some2Event.MOUSE_UP while stopping the MouseEvent.MOUSE_UP on Some2. The coercion error is between Some2Event and Some1Event. With this error I've tried the other #1034 solutions:

  1. Adding an instantiation of Some1Event and Some2Event in the main application.
  2. Wrapping with 'new Event' - I am unable to dispatch the event as type Event, as it has custom properties.

I've also tried the following code:

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

but it didn't report anything.

While dispatching the event Some2Event, I get the error

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]

The code is structured as Some2 (Sprite) has many children of Some1 (Sprite). In Some2, I am listening for a MouseEvent.MOUSE_UP event on the Some1 Sprite, and in that listener, stopping the propagation, and dispatching a custom Some2Event:

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

In Some2, I have the following registered event listeners:

    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
    }

The Some1Event and the Some2Event are nearly the same:

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

}

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

}

Any help is definitely appreciated.

Was it helpful?

Solution

Constants in Some1Event:

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

Constants in Some2Event:

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

The constant value is the same in each case. 'MOUSE_DOWN' or 'MOUSE_UP' . How do you expect Flex to tell the difference between the two? It can't! Change the constant values to be unique

In Some1Event:

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

In Some2Event:

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

Now Flex will view the two events as different / unique and won't call a listener for one event when the other is dispatched.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top