views:

363

answers:

2

I have a class essentially:

public class WindowEvent extends Event
{
    public static const WARNEVENT:String = "warnEvent";
    public static const TASKREQEVENT:String = "taskRequestEvent";
    public static const TASKANNOUNCE:String = "taskAnnounce";
    public static const WINDOWCHANGE:String = "windowChange";
    public static const ILLEGALPOSITION:String = "illegalPosition";

    // insert brevity   
}

The first four events were working fine, but I just I added ILLEGALPOSITION and tried this:

    // inside Window.as
    private function checkDimensions():void {
       if(!Window._legalBoundaryEnable)
           return;
...    var pass:Boolean = Window.legalBoundary.containsRect(
455        this.getBounds(stage));
456    if(!pass) {
457        this.dispatchEvent(new WindowEvent(WindowEvent.ILLEGALPOSITION,
...           "Illegal Position etc."));
       }
    }

So Flex spewed this stack at me when I hit the dispatch method:

TypeError: Error #1034: Type Coercion failed: cannot convert ¬
     flex.utils.ui::WindowEvent@511dce41 to flash.events.MouseEvent.
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at mx.core::UIComponent/dispatchEvent() ¬
     [C:\autobuild\~\UIComponent.as:9298]
    at flex.utils.ui::Window/checkDimensions()[C:\Users\~\Window.as:457]
    at flex.utils.ui::Window/stageResized()[C:\Users\~\Window.as:220]

As you can see from the trace, Window.as:457 is the last user code line. So WTF is flash.events.EventDispatcher.dispatchEventFunction trying to do with a MouseEvent?

+1  A: 

Try using another value for ILLEGALPOSITION, "illegalPosition" might be used by Flex itself (or another part of your code) and related to a mouse event. So when that event handler fires, it tries to convert your event to a MouseEvent because it thinks it should be one.

Samuel
Yeah, I learnt that one the hard way...If I had the reputation I'd +1 you.
Ben
+5  A: 

That error normally occurs because a listener you have set up has incorrect event param type. I'm pretty certain that this must be the case here.

Check all the listeners you have set up for that event and make sure the function is

someFunction(event : WindowEvent) : void
James Hay
I had duplicated a listener function and you were right, the parameter type was <code>MouseEvent</code>.Was a silly mistake really, wasn't it.
Ben
/takes it you can't use tags in comments.
Ben