1

I've been looking at answers for my problem, I think I may be on the right track but I just can't understand how to implement the solution. I need help.

I have a class, RootPackage.Utils.SelectableGraphic.as, a widget, RootPackage.Widgets.WidgetWithSelection.mxml and a custom event, RootPackage.Events.SelectableGraphicEvent.

The SelectableGraphic dispatches SelectableGraphicEvents and the WidgetWithSelection listens to those events. My event looks like this :

{
import RootPackage.utils.SelectableGraphic;

import flash.events.Event;

public class SelectableGraphicEvent extends Event
{
    public static const GRAPHIC_ADDED:String = 'Graphic_Added';
    public static const GRAPHIC_CLICKED:String = 'Graphic_Clicked';

    private var graphic:SelectableGraphic;

    public function SelectableGraphicEvent(type:String, gra:SelectableGraphic, bubbles:Boolean=false, cancelable:Boolean=false)
    {
        super(type, bubbles, cancelable);

        graphic = gra;
    }

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

    public function get Graphic():SelectableGraphic
    {
        return graphic;
    }
}

My dispatch looks like this :

lastDrawnGraphic = new SelectableGraphic(...);

lastDrawnGraphic.dispatchEvent(new SelectableGraphicEvent(SelectableGraphicEvent.GRAPHIC_ADDED, lastDrawnGraphic, true));

And my listening function looks liek this :

private function SelectableGraphic_ClickHandler(event:SelectableGraphicEvent):void
{
    gra:SelectableGraphic = event.Graphic;

    ...
}

It crashes between the dispatch and the reception in the WidgetWithSelection, before entering the listening function. If I change the listening function header to listen to a basic Event, then it doesn't crash but, when I try to obtain the event.Graphic, it gives the same type coercion error. I've tried many work arounds, for example receiving a normal event, casting it to a SelectableGraphicEvent and using it. The cast operation returns a null value in this case.

The type coercion error is something like : Error #1034: Type Coercion failed: cannot convert RootPackage.Events::SelectableGraphicEvent@b56b071to RootPackage.Events.SelectableGraphicEvent.

Anyhow, I've read a bit about this and I believe it might be due to those classes being in a different application domain than my main application or something to that effect. Apparently there's a way of changing the application domain but I just don'T know how to do this. This has me confused at the moment, as you probably can tell from the last paragraph. Is there a simpler answer or can anyone explain to me what I should be doing?

Thanks,

Ggilmann

EDIT TO ADD:

Here is the part where the EventListener is added. The open() is on the Added_To_Stage event and the close() is on the Remove_From_Stage :

private function open():void
{
    this.map.addEventListener(SelectableGraphicEvent.GRAPHIC_CLICKED, SelectableGraphic_ClickHandler);
    this.map.addEventListener(SelectableGraphicEvent.GRAPHIC_ADDED, SelectableGraphic_AddedHandler);

    _selectableGraphics = GetSelectableGraphics();
}

private function close():void
{
    this.map.removeEventListener(SelectableGraphicEvent.GRAPHIC_CLICKED, SelectableGraphic_ClickHandler);
    this.map.removeEventListener(SelectableGraphicEvent.GRAPHIC_ADDED, SelectableGraphic_AddedHandler);

    ClearSelection();
}

Basically, there's a map that contains a graphic layer that contains the selectable graphics. So when I dispatch from my SelectableGraphic, the event bubbles up to map (and beyond). The map catches the event and fires the appropriate SelectableGraphic_EventHandler.

Ggilmann
  • 79
  • 1
  • 11
  • Are you loading modules, or are you creating your own application domains, if not this shouldn't be your concern. In the handler you should use your type for the event, where is your addEventListener code? Also make sure you use flash debug player when running your code and paste error messages here as you work through them. – shaunhusain Jul 26 '12 at 17:30
  • Thanks for the reply. As your first question goes, I don't know what answer I can give you. I don't think I'm creating application domains and I've written no code to load modules. However, there is a big part in the app that I haven't written myself. – Ggilmann Jul 26 '12 at 17:34
  • My handler is using the same event type I believe, or maybe I've missed something? Ahh, you want to see the AddEventListener code, gimme a sec, I'll edti my OP – Ggilmann Jul 26 '12 at 17:36
  • I am using the flash debugger, problem is, it's in french so even if I copy the error word for word here, you'll probably not quite get it. – Ggilmann Jul 26 '12 at 17:39
  • Very strange it looks like you have everything in order, and yeah like you guessed I don't know French, I could pass it by my mom but she doesn't know AS3 :). Since you didn't write most of the code and based on the error and seeing that everything you have setup looks correct my guess is that as you stated originally this has something to do with the instance of the Class doesn't match some other definition of the Event class that it has loaded. Did you write the new event class? Also how are you building is the project mavenized or using ANT or just in Flash(Builder) IDE. – shaunhusain Jul 26 '12 at 18:54
  • Everything you see in the post, I wrote. So I wrote the SelectableGraphicEvent. It's based on flash.events.Event so I'm not sure what other event class it could be mismatched with. And I'm only using Flash Builder. I read somewhere that if you have a swf that loads other swf in your code, it can cause problems like this. I also read that this error can be solved by doing something about the ApplicationDomain though I could never understand what exactly. I'll try to find a link so people can see what I'm referring to. – Ggilmann Jul 26 '12 at 19:07
  • Ok I just found a possible fix (http://forums.arcgis.com/threads/11960-com.esri.ags.symbols.TextSymbol-is-not-TextSymbol-in-FlexViewer-widget) which is directly related to the api im using. Apparently somewhere in the code, widgets are getting loaded into separate application domains and that causes the problem. I'm testing right now. – Ggilmann Jul 26 '12 at 19:14
  • Otherwise, blogs like these put me on the trail of application domain problems : http://blog.nobien.net/2009/01/14/type-coercion-failed-of-the-same-class/ or http://nwebb.co.uk/blog/?p=375 – Ggilmann Jul 26 '12 at 19:17
  • Well, it seems that the first link's fix did the trick. If you want, since you're the only one that even tried answering, you can just write up something as an answer and I'll mark it as solved – Ggilmann Jul 26 '12 at 19:20

0 Answers0