When you want to use primitives with mouse events you need to place them in a group, in the group you set mousetransparency to false so that only the primitives within the group react to the mouse. This goupr has a small triangle element the mousedown event only triggeres when the mouse is over the triangle.
<s:Group id="sizer" bottom="1" right="1" width="16" height="16" mouseDown="sizer_mouseDownHandler(event)" mouseEnabledWhereTransparent="false">
<s:Path data="M 0 16 L 16 16 L 16 0 L 0 16 z">
Thanks for your help. It seems that this approach will work for me. I
still have one more question. In my case every primitive needs
different event handler (and group object in this approach). Is this
the most efficient solution? It seems a lot of living object in memory
The over head for each group wouldn't be that dramatic, even if you decided to draw each of the elements directly into 1 group you would need to have some sort of co-ordinate calculation to work out which element you were over and the matter of transparency would complicate that calculation, what you could do is have all the objects responding to 1 mousedown listener and use a case statement based on currenttarget to perform the required operation. At the end of the day the method I suggested works well as you can easily locate the object where you want, and their transparent areas can overlap without effecting the mouse event behaviour.
This is an interesting problem. Up to now I've been using method 1, extending mouse event functionality by a wrapping the primitive w/in a Group container, but I wondered whether this was the most efficient method. Certainly, it is the easiest to code, but ideally it would be nice if the objects themselves could dispatch their own events.
The problem is spark.primitives are, and only use, very low-level objects (to keep size to a minimum) and we need the functionality of an InteractiveObject, or its most concrete class, Sprite (Group IS A Sprite btw).
As for method 3, it is not enough to extend spark.primitives.Rect because, while we can dispatch events, how do we tell the object when to dispatch a mouse event?? What we really want is Sprites not primitives, but it would be nice if everything could be used as we are accustomed. So I thought maybe we could code am InteractiveFill/ InteractiveStroke that would extend Sprite and be used in place of the normal fill/stroke classes.
Managed to do this, and it displays correctly, but the events still aren't firing
Maybe when I get this up and running, it would be nice to randomly toss 1000s of primitive objects onto the stage and see whether Group or InteractiveFills/Strokes perform better. Will let you know,
PS - I don't think method 2 is viable at all.
PPS - tip: rather than use group, you can use SkinnableContainer, and wrap strokes/fills w/in separate groups to trigger different events for each.
1 person found this helpful
Bah, the fill and stroke classes aren't what I thought they were -- they're just methods on how to fill/stroke a graphics objects...
so I yield.
You need to either wrap the primitives w/in a container that is an InteractiveObject and that can easily be attached to the display list (hence anything extending UIComponent -- Group or SkinnableContainer are good choices)
or, you need to create your own Sprites and draw the graphics yourself.
Sorry I couldn't be of more help here.
Thank you both. I will use Group containers.