6 Replies Latest reply on Nov 29, 2009 12:39 PM by Crt Gorup

    spark.primitives and mouse events

    Crt Gorup Level 1



      I am wondering which is the best way to handle mouse events (clicks) and graphic primitives (lines & squares). I created a set of primitives and I added them on a spark Group container. Now I would like to attach mouse event handler to every shape, but mouse events are not supported on spark primitives. What is the best solution to this problem. I came across to the following ideas.


      1) Wrap every primitive in separate Group component and attach mouse event handler to it.

      2) Store the positions of the primitives in a list and attach mouse event handler to the main Group component which holds everything together. Event handler would then have to compare mouse x,y to the coordinates stored in a list.

      3) Extend spark.primitives.Rect class to custom component which would support mouse events. I have no idea how to implement this.


      I am a beginner in Flex, so any suggestion is more than welcome.


      Thanks, Crt

        • 1. Re: spark.primitives and mouse events
          David_F57 Level 5



          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">


          <s:LinearGradient rotation="45">

          <s:GradientEntry color="0xEDEDED"/>

          <s:GradientEntry color="0xCDCDCD"/>









          • 2. Re: spark.primitives and mouse events
            Crt Gorup Level 1



            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

            to me.


            Thanks, Crt

            • 3. Re: spark.primitives and mouse events
              David_F57 Level 5



              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.



              • 4. Re: spark.primitives and mouse events
                mewk Level 3



                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,


                - e



                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.

                • 5. Re: spark.primitives and mouse events
                  mewk Level 3

                  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.


                  - e

                  1 person found this helpful
                  • 6. Re: spark.primitives and mouse events
                    Crt Gorup Level 1

                    Thank you both. I will use Group containers.