4 Replies Latest reply on May 10, 2011 3:15 PM by Flex harUI

    how to get event from flash component via mxml

    dsdsdsdsd Level 1

      hello;

       

      in flash I made a simple mc: when it is clicked it dispatches a custom event ... works fine.

       

      I have exported to .swc

       

      I used <local:my_button /> ... works fine ... when button is clicked the trace command from within the flash button fires in the flex console.

       

      PROBLEM: I want to capture the mc_button's custom event using <local:my_button   capture_it_here_and_handle_it  />. currently when I type within that tag, code hinting does not recognize any of the properties of the custom event.

       

      I am basing my approach on this tutorial video: http://tv.adobe.com/watch/under-the-hood-with-adobe/create-custom-flex-components-with-fla sh-cs3/

       

      he did not use <fx:Metadata> in his mxml ... he did not show his custom event class so I do not know if he did any flex-related metadata in there or not.

       

       

      any thoughts?

       

      thanks,

      dsdsdsdsd

        • 1. Re: how to get event from flash component via mxml
          Flex harUI Adobe Employee

          The class definition in flash would need Event metadata.

          • 2. Re: how to get event from flash component via mxml
            dsdsdsdsd Level 1

            when flash compiles, it will compile the .as class, and it may all happen on someone else's computer, maybe a designer's...

             

            when I bring the .swc file onto my computer in my working directory, do I need to bring the .as file also?

            • 3. Re: how to get event from flash component via mxml
              dsdsdsdsd Level 1

              MY SOLUTION:

               

               

              ISSUES that I ran into:

              A - I was putting the  [Event ...] metadata into the MyEvent.as file ... WRONG ... should be a  part of the component itself ... for me that meant actually on the  timeline script inside my mc ... ( if your component is created via your_mc.as, then you will add the [Event ...] directly above your Class declaration ).

              B - getting the mc compiled as .swc ... not difficult but did require downloading some flex-plugins for flash ( free ) ... plenty of tutorials on google

              C - understanding the 'local' namespace in flex ... I think that it is a reference to the compiler setting for library path that points to .swc ... what happens if you have more than one .swc, each coming from different library paths ... I don't know.

              D - Event is case sensitive ... [Event ... ] != [EVENT ... ]

               

               

              3 files:

              in directoryA ( maybe your designer's computer ) ...

              1 - flash file with movieclip ... .fla will be exported as .swc with your mc inside it with linkage_id

              2 - custom event sub-class ... my_event.as

               

              in directoryB ( probably your eclipse/flashbuilder project folder ) ...

              3 - my_flex.mxml

               

               

               

               

               

              codes for each of the 3 files

              ....................... 1 .... the movieclip ( for me I am actually on frame 1 of the timeline inside my mc ) ...

              [ACTIONSCRIPT]

              [Event( name="box_picked", type="pkg_application_2.ece_box")]
                 
              import flash.events.MouseEvent      ;

              import MyEvent                             ;

               

              addEventListener( MouseEvent.MOUSE_DOWN , mcm_handle_mouse_down ) ;
              function mcm_handle_mouse_down( e : MouseEvent ) : void
                { trace ( "mcm_handle_mouse_down " ) ;
                 
                  if ( e.target != e.currentTarget )
                    {  const lkn   : String      = e.target.name                                                              ;
                      const lko_e : MyEvent = new MyEvent( lkn , MyEvent.SOME_TYPE , true , true )  ;
                     
                      dispatchEvent( lko_e ) ;
                    }
                } ;

               

              [/ACTIONSCRIPT]

               

              ....................... 2 ... MyEvent.as ( extends Event )

              [ACTIONSCRIPT]

              package
                {
                  import flash.events.Event;
                 
                  public class MyEvent extends Event
                    {
                                   public var    pvs_something : String                        ;
                          static public const SOME_TYPE  : String = "some_type" ;
                         
                         
                          public function MyEvent ( args_type       : String          ,
                                                               argb_bubbles    : Boolean = false ,
                                                               argb_cancelable : Boolean = false   ) : void
                            {
                              super( args_type , argb_bubbles , argb_cancelable ) ;
                              pvs_something = "boo" ;      
                            }
                    }
                }

              [/ACTIONSCRIPT]

               

               

               

              ....................... 3 ... my_flex.mxml

              [MXML]

              <?xml version="1.0" encoding="utf-8"?>
              <s:Application xmlns:fx              = "http://ns.adobe.com/mxml/2009"
                                   xmlns:s               = "library://ns.adobe.com/flex/spark"
                                   xmlns:mx              = "library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
                                  xmlns:local           = "*"
                                 >
                 
                  <fx:Script>
                      <![CDATA[ 
                          public function om_handle_components_custom_event( e :  Event ) : void 
                            { trace( "flex.om_handle_box_picked , A : " + e          ) ;
                              trace( "flex.om_handle_box_picked , B : " + e.target   ) ;
                              trace( "flex.om_handle_box_picked , C : " + e.pvs_something  ) ;           
                            }
                      ]]>
                  </fx:Script>
                 
                 <local:my_mc_linkage_id    some_type = "om_handle_components_custom_event( event )"       />
                  
              </s:Application>

              [/MXML]

               

              other helpful links...

              http://livedocs.adobe.com/flex/3/html/help.html?content=createevents_3.html

              http://tv.adobe.com/watch/under-the-hood-with-adobe/create-custom-flex-components-with-fla sh-cs3/ ... easy 7 minute video

              • 4. Re: how to get event from flash component via mxml
                Flex harUI Adobe Employee

                The AS file with the metadata should've been packages into the SWC