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.