Michael Gregor – Interactive Television Pioneer

Flex Authoring for Flash (AS3 Project / SWC)

September 5, 2008 · 9 Comments

!!!

NOTE: Flash CS4 allows for SWC linking, dating some of  this post  to CS3 and previous.

!!!

Forward: Flex AS3 projects publish SWFs and as I understand do not automatically include the Flex RIA framework. It is sort of like authoring for Flash in Flex. If you are going to eventually publish with Flash then be sure to be careful what packages you reference with these projects, some might not be able to be compiled with Flash.

Let’s face it, Flex basically sucks for complex symbol production, and Flash basically sucks for development. Flash kicks butt for complex symbol production and Flex kicks butt for developing. So use both!

Recently I have had the opportunity to work in an environment that supports engineering best practices. In my years of being an architect and developer, I find that all work in the UI space is iterative and that those iterations also apply to process and best practice development. Having the ability to iterate allows for refinement, clean up, optimization, smarter class hierarchies, and significant satisfaction in ones work. Iteration of production process seems to have real payoff by reducing the amount of work that is duplicated from one stage of a UI’s “life” to the next.

My current projects start life as Illustrator AI or Photoshop PSD UI comps that are generally produced for flash consumption.  What I mean by that is that the layers that make up an element are often grouped, and elements and components are logically organized.  This is a tremendous help and if you are smart and lucky enough to work with cooperative designers, you could even provide a naming convention and layer and grouping scheme that could save *you* LOTS of time.  The catch here is that you are not necessarily saving total project time, because the designer is now responsible for at least the first iteration of naming the elements you will be importing.  You might also show your designer how to configure AI or PS to snap to the nearest whole number pixel and how strokes are better as shapes instead of shape outlines.   Add all of these ideas together and you are going shave some significant Flash production time.

Flash’s AI and PSD import filters are a life saver, they provide many options to import the various layer types that AI and PSDs support.  The importer allows you to choose just what to do with every bitmap, vector, and text layer as well as every group.  You can convert each one of these layers to its compatible Flash symbol type.  You can preserve editable text, rasterize it, or create outlines.  You can rasterize vector shapes, and you can choose the compression type and amount for each bitmap element.  You can create movieclips from a groups.  Another nice feature is the import filter respects the visibility of layers in the source file, making it easy to import individual components without having to hunt around for the correct layers within the import dialog.   One can use a design comp that has many components and panel backgrounds in it and just switch on the layers that represent a component set, save, then import.  Best of all you can set your default import preferences for the various element types in the Flash preferences panel.

The amount of work you have to do from there is based on the complexity of the UI elements, components, and panels, as well as how well you are staged as a result of the designer properly following your production guidelines. You will still have to do some “mechanical” production to achieve your desired interactivity and behavioral capabilities.

Flash’s WYSIWYG composition tool is great to make the small adjustments that are often necessary to tighten up a design for production as well as creating symbols for items that are used dynamically.  Now all you have to do is give each symbol a unique and logical linkage class (AS3) or identifier (AS2) name and publish the FLA as a SWC and you can then access these library elements as classes from Flex and other tools like Flash Develop.  A nice shortcut is to create a symlink from the Flex Libs folder to your Assets SWC.  This allows you to put your FLA file where you want it and not have to create a custom path for your SWF/SWC output.  More on this later.

Let’s all admit that editing code with Flash is not the best experience and leaves a lot to be desired.  I have not tried to debug in Flash for a looooong time, but I still believe that debugging in Flash is no where near as capable as debugging in Flex.   Another reason to publish to SWC and code in Flex is that Flash does not support adding SWCs to the class path.  This simply defeats the OO principal of having libraries that you can “re-use”, in this case they are simply UI class libraries.  So if you want to share some symbols from one FLA to another you need to either copy the elements from one library to the other, which can require some library maintenance and might cause some collision on symbol and class naming (BAD), or create components to ’share’ and import those as compiled clips, this way there is only one library symbol for the various symbols that make up that component (GOOD).  My current project requires that I be able to publish in Flash *and* Flex.   Flash to create stand alone Mac and PC projectors, and Flex for my preferred development environment.  Creating components in my shared UI FLAs and importing those into my main FLA is quite easy thank goodness…

Master Components – A nice shortcut for sharing the entire contents of a SWC (or any subset really) is to create a symbol and drag all of the library items desired into that one symbol.  Create a component from that symbol, publish a SWC to the correct Flash folder, refresh your components panel, drag that one component into your main project FLA’s library.  This SWC will contain every element with their class / linkage names, and they will be accessible from AS.   I am not sure what happens if you have a naming collision, but my guess is the symbol in the library will take priority over the symbol in the components ‘library’.

On to Flex:

Flex loves SWCs!   It hooks all of the classes compiled into a SWC, which in my case are all Flash UI symbol classes.  Flex adds these classes to the code hints list automagically, THIS IS HUGE!

I use a “_mc” naming convention on all my UI symbol class names so it is easy to differentiate skins from code classes when coding.  I always have a code class that instantiates its companion UI class as its “clip_mc” var (class composition) . For example a button skin is a Flash UI class named “btn_close_mc” and this would be instantiated as the “clip_mc” var on the “Btn_close” code class. This way of skinning provides for great flexibility and even inheritance and overriding of skin elements. More to come on this in another post… Stay tuned!

Though I am not using any code classes in SWCs for my current work, SWCs can also be a great way to package and even protect entire code libraries.

Developing in Flex is “real” development. It is Eclipse development. The same development environment used for many languages and by many ‘professional’ software developers. Flex (Eclipse) has a great editor, debugger, watcher, and profiler. It has real time build error detection and the problem report panel save lots of time – just try it!   Flex scans your SWCs and code classes and hints them!  It also has:  SVN integration via Subclipse, Multiple project support – Project tree view, Cut and paste files into project view, Refactoring, Find in Files across multiple projects, ASdoc support, Comment and Function collapsing, Syntax highlighting…  The list goes on and on…

Now back to the symlink. As I mentioned earlier I have a requirement to be able to use Flash or Flex to compile and publish my project. Ultimately my project is published to Mac and PC projectors and as I understand it, of the two, only Flash can do this. This is where the trickery comes in. If the the FLA file lives in the root of my Flex projects “src” folder and not in the libs folder then I do not have to change any publish settings for the SWC or move the code classes (com) from Flex’s src folder into the libs folder to compile the project with Flash.

FEATURE REQUEST! – a separate line in the publish preferences for SWCs.  Log that “issue” in JIRA!

Final Thoughts:

Why wouldn’t you use the best tool for the job at hand? In this case, you have to buy  (Try – ad infinium) both tools, but the time saved and the sanity maintained is more than worth the investment (or not).

Building and maintaining complex components in Flex is a PITA, use Flash.

Flex is a feature rich software development tool that promotes professional development practices, don’t use Flash to develop.

Note:

SWCs and Flex are not limited to AS3 project types. MXML provides for pixel accurate reproduction of complex components using the various compatible asset types, but I don’t see any advantage to rebuiling entire UIs in MXML when you can use components imported from design files into Flash and published to SWCs. I will have to try this approach on my next Flex framework based RIA and make sure it is best there as well.

Categories: Work Related
Tagged: , , , , , , , ,

9 responses so far ↓

  • The Flash Design Guy // September 5, 2008 at 2:53 am | Reply

    Thanks for the article. I’m slowly learning Flex and moving certain complex tasks from Flash to Flex and it’s good to hear that i’m doing the right thing on this

  • Flex Authoring for Flash (AS3 Project / SWC) « Rich Internet Applications // September 5, 2008 at 4:30 pm | Reply

    [...] Source [...]

  • zack // October 4, 2008 at 3:04 pm | Reply

    Nice article! Very interesting.

    I have been developing my project in Flash for a while now, and would like to move it out as much as possible. Reading your article makes it sound possible, but I am not quite sure how to do it. I was wondering if you would care to give some advice. I believe my situation is very similar to others who are trying to find a good balance between Flash and Flex/Eclipse:

    I have:
    - Main.fla which holds all my interface and their layout. It also contains two scenes with a preloader-logic in the first .
    - Lots of .as files, linked via the “Export for actionscript” option.

    I would like to:
    Move everything out and use flex to compile with all the stuff in an .swf. Is there a way to achieve this? Do you know where I should start looking?

  • zack // October 4, 2008 at 3:05 pm | Reply

    correction: compile all the stuff via an swc*

  • crunchbucket // October 6, 2008 at 3:13 pm | Reply

    Thanks Zack.

    From what you have told me. You might have luck getting in to Flex by the following.
    1) create a new Flex project
    2) copy your current project into the src folder
    3) Remove the “Document Class” definition from the stage properties (in the props panel) of your Main.fla. Make sure everything you want to reference programatically is set to export.
    4) Publish the SWC and move it to the /libs folder. (Libs is a sibling of src at projects top level). This makes Flex aware of those compiled elements.
    5) Flex should find the code classes that you declare based on your package structures.
    THIS IS WHERE THINGS MIGHT BREAK DOWN WITH YOUR CONFIGURATION OF EXTERNAL CODE CLASSES AS I AM NOT SURE WHAT YOU MEAN HERE.
    6) Flex will find the SWC and hint its contents. I like to remove all the code from my UI SWCs by making all elements in my SWCs only be UI classes that extend Movieclip. There are many reasons for this, but the best on being that if you have code classes compiled into a SWC that are the same as the classes in your packages and you modify them, there will inevitably be some wasted time figuring out how to deal with that situation. I am not sure which one takes precedence, the one in the SWC, or the one in the package, but they have the same “path” as far as Flex is concerned.

    Good Luck,

    MG

  • zack // October 22, 2008 at 6:44 pm | Reply

    great explanation, thanks a lot!
    This is exactly what I meant. This might take a bit of work trying out.. ;)

  • eRez // February 11, 2009 at 1:07 pm | Reply

    hi,
    though its been a while since this post was posted, i just ran into it now while looking for a solution for a problem i have. it goes like this:
    i use flex to create and compile my projects, and flash to create graphic data. each graphic data is placed in a symbol to which i add linkage and then export it all as SWC, which i then link to my flex project so all the graphic data is available from within flex.
    however, i have all those different graphic containers (created in flash), and for each of them i have a class (created in flex) which extends it. for example – a graphic container with linkage id ‘ProductContainer’, and then ‘class ProductContainerEnhancer extends ProductContainer’ (so that all the mc’s/buttons/txt’s within ProductContainer are available to ProductContainerEnhancer). eventually all those graphic containers have some properties and methods in common, so i thought that instead of having them extend MovieClip (as they do by default), why not create in flex ‘class ContainerTemplate extends MovieClip’ which will hold all the common properties and methods, and have every container’s base class to be that class. once i do that, the ‘ProductContainerEnhancer’ doesn’t recognize anymore all the mc’s/buttons/txt’s inside ProductContainer.
    hope i made myself clear and that i’ll get any sort of help.
    thanks in advance,
    eRez

    • crunchbucket // February 11, 2009 at 5:22 pm | Reply

      eRez,

      How about leaving the “Classes” in Flash as they are, extending Movieclip. (I think you are still doing this)
      Then your Flex Code classes will have properties for a clip_mc and all of the elements within that clip.
      protected var clip_mc:Movieclip
      protected var textfield1_txt:TextField;
      protected var textfield2_txt:TextField;

      It will at some point (most likely the constructor)
      assign the local references to the clip_mc’s elements
      clip_mc = new ClassFromFlash(); – this is the “Class” in the SWC
      textfield1_txt = clip_mc. textfield1_txt;
      textfield2_txt = clip_mc. textfield2_txt;
      (do this for every element in the Flash MC)

      Now your Flex class can act as it is the Flash class via references.

      Does that make sense?

      MG

  • eRez // February 12, 2009 at 8:07 am | Reply

    thanks! it makes sense and it even works.
    now i have in flash MC with linkage ‘Symbol1′ (extending MovieClip), in it textfield ‘_txt’ and background ‘bg_mc’. i have in flex 2 classes: ‘Symbol1Template’ with 2 protected vars ‘_txt’ and ‘bg_mc’; and class ‘Symbol1Enhancer’ which extends ‘Symbol1Template’, in which constructor i create a new ‘Symbol1′ and then link its _txt & bg_mc to those in the template.

    now it works.
    thanks again,
    eRez

Leave a Comment