AS3: Linked MovieClips from remote SWFs. →← TextMate and mxmlc

Flash IDE for Designers - Flexbuilder for Developers!

I used Flexbuilder for a couple of big AS3 and Flex projects and once you start to develop in Flexbuilder (Eclipse) you maybe don’t wanna go back and use a normal Text Editor. So i was looking for a way to use Flexbuilder for heavy graphical sites that also need the Flash 9 IDE.
I think the best way to work on big projects is for designers to work in the Flash IDE and for Developers to add functionality with a professional programming tool like Flexbuilder.
I was playing around with swc’s but it just didn’t work the way a wanted it to work.
Josh came up with the idea of loading swf-files and accessing the assets through the swf, not through a swc. I tested it the way I like it to use in Flexbuilder and it works perfectly (ie. code completion and code checking still works). You can add all functionality after you compile the asset-swf. This is the perfect way to work hand in hand with designers.

So here are the basic steps:

First prepare your assets in Flash 9

  1. Create a new AS3 file. Create a MovieClip and add a Class-Name (like ‘com.bigspaceship.test.GreyRectangle’) in the Linkage Property Window of the Movieclip.
  2. Create a minimal version of the Class:

    package com.bigspaceship.test{
     import flash.display.MovieClip;
     public class GreyRectangle extends MovieClip{
      public function GreyRectangle() : void {
      }
     }
    }

  3. Compile/Publish the swf.





Within Flexbuilder: adding the functionality

  1. Create a new AS3 project and add the same classpath that you used in your Flash 9 File (com.bigspaceship.test).
  2. Embed or load your Asset.swf in your Main-Class. The Embed tag only works in Flexbuilder so if you also wanna compile it from Flash 9 you should load the asset.swf.

    Note: you cannot access the embedded assets inside the constructor so i added a little work around to initialize my assets after the constructor is called.

    Example for a document/main Class embedding the swf (Flex only):


    package{
     import com.bigspaceship.test.*;
     import flash.display.MovieClip;
     import flash.events.Event;

     public class MainFB extends MovieClip{
      [Embed(source=’../bin/assets2.swf’)] private var AssetsClass : Class;

      public function MainFB():void{
       // instantiate the embeded assets2.swf-Class to access it’s library.
       // it doesn’t work without that line of code.
       var assets : MovieClip = new AssetsClass() as MovieClip;

       // workaround to init my assets after the constructor call.
       // maybe you know something better. it works good for me.
       this.addEventListener(Event.ENTER_FRAME, this._myInit);
      }
      private function _myInit($evt:Event):void{
       // removeEventListener to make sure that this function is only called once.
       this.removeEventListener(Event.ENTER_FRAME, this._myInit);

       // create an instance of the BlueRectangle MovieClip
       // that is inside the assets2.swf library
       var b:BlueRectangle = new BlueRectangle();
       b.y = 200;
       addChild(b);
      }
     }
    }



    Example for a document/main Class loading the swf (works in Flex and Flash 9):

    package{
     import com.bigspaceship.test.*;
     import flash.display.MovieClip;
     import flash.events.Event;
     import flash.net.URLRequest;
     import flash.display.Loader;

     public class MainFBFL9 extends MovieClip{

      public function MainFBFL9():void{
       // loading asset library swf
       var assetLoader:Loader = new Loader();
       assetLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, _onAssetsLoad, false, 0, true);
       assetLoader.load(new URLRequest(”assets1.swf”));
      }

      private function _onAssetsLoad($evt:Event):void{
       // called when assets1.swf is completely loaded
       // create an instance of the BlueRectangle MovieClip
       // that is inside the assets1.swf library
       var r:GreyRectangle = new GreyRectangle();
       addChild(r);
      };
     }
    }

  3. Now you can add functionality to your Movieclip:

    package com.bigspaceship.test{
     import flash.display.MovieClip;
     import flash.text.TextField;
     public class GreyRectangle extends MovieClip{
      /*
    Define all variables whose instances are created within your GreyRectangle MovieClip in the Library. All vars are already instantiated within the MovieClip. So, if you instantiate one of them again you are going to overwrite the variable and lose the reference to the Object that is already within your MovieClip
    */
      public var tf : TextField;
      public var bg : MovieClip;
      public function GreyRectangle() : void {
       tf.text = 'hello world !';
      }
     }
    }

  4. Compile your Project from Flex and be surprised that actually everything works!
    It seems like Flash is now compiling all Classes again and uses them instead of the Classes that are compiled within the asset.swf.



I put together some example files that play with different asset-files and nested MovieClips:
Source Files

Have fun!

EDIT: 11/13/2007
To get that all to work it is important to uncheck “Automatically declare stage instances” within “AS 3.0 Settings” in your Assets.fla file.

AS 3.0 Settings



November 9th, 2007  by Stephen  /  15 Comments

Comments on “Flash IDE for Designers - Flexbuilder for Developers!”

  1. ” // workaround to init my assets after the constructor call.
    // maybe you know something better. it works good for me.
    this.addEventListener(Event.ENTER_FRAME, this._myInit);

    you can do that or dispatch INIT in the GreyRectangle class
    and listen for the INIT in the MainFB class

    package com.bigspaceship.test{
    import flash.display.MovieClip;
    import flash.events.Event
    public class GreyRectangle extends MovieClip{
    public function GreyRectangle() : void {
    dispatchEvent(new Event(Event.INIT))

    }
    }
    }

    then

    this.addEventListener(Event.INIT, this._myInit);

    in MainFB

    I hope this looks right it’s hard to type in this small box.

    Josh on November 9th, 2007 at 7:28 pm
  2. @Josh: good idea! also i’d prefer to keep everything in just one class if possible

    Daniel on November 9th, 2007 at 8:15 pm
  3. If you like to keep everything in one class, why not import the swf at runtime and compose it in a class you create in flex builder? You can cast the loaded content into a MovieClip and discard the Loader object. Just leave only frame labels, stop frames and only the (ugh) timeline based ActionScript in the SWF file. Of course it wouldn’t work for instances where most of the code can’t be seperated from the timeline. But for most animations it would let you keep all of your ActionScript out of your SWF and in flex builder. Also, since you’re importing assets at runtime rather than embedding them, you get better control over when and where you load your files. A side effect of this is that the “clean up” phase of the designer’s FLA can be reduced since the SWF itself needs less logic embedded within it. Then, when you need to use a clip a number of times, you can embed it into the app or create a shared library that you load at runtime.

    Anyways, really great read. I always like to see how people come up with clever ways to free themselves from the shackles of the Flash IDE.

    Chris on November 10th, 2007 at 3:59 am
  4. […] Found this on bigspaceships blog: Flash IDE for Designers - Flexbuilder for Developers! […]

    Lednine » AS3 for designers and developers on November 10th, 2007 at 9:58 pm
  5. Use FDT, flex builder is rubbish compared!

    gabes on November 11th, 2007 at 1:35 pm
  6. I created the ‘Create Classes Command & Panel’ for projects we were working on that require Flash assets that you nay find useful.

    http://www.tink.ws/blog/create-classes-command-panel/

    We also have LibraryManager for loading/embedding and accessing all assets where you can create multiple libraries. I’ll get round to blogging this over the next few weeks.

    Tink on November 11th, 2007 at 2:01 pm
  7. […] BIG SPACESHIP LABS / » Flash IDE for Designers - Flexbuilder for Developers November 12th, 2007 […]

    trace(chris.foster) » BIG SPACESHIP LABS / » Flash IDE for Designers - Flexbuilder for Developers on November 11th, 2007 at 9:22 pm
  8. […] Bigspaceship uses Flexbuilder for a couple of big AS3 and Flex projects and once you start to develop in Flexbuilder (Eclipse) you maybe don’t wanna go back and use a normal Text Editor. So i was looking for a way to use Flexbuilder for heavy graphical sites that also need the Flash 9 IDE. BigSpaceShip thinks…. http://labs.bigspaceship.com/blog/?p=76read more | digg story […]

    flashalisious » Blog Archive » Flash IDE for Designers - Flexbuilder for Developers! on November 13th, 2007 at 3:47 pm
  9. […] http://labs.bigspaceship.com/blog/?p=76#more-76 […]

    USE FLASH MORE » Blog Archive » Reusing Flash CS3 Assets in Flex and Other Applications on November 14th, 2007 at 11:00 am
  10. I haven’t used the exact method you describe but I’ve been using Flex2 SDK and Eclipse for a couple BIG projects - mixture of compile and runtime asset loading. We’ve been using SWF libraries for various sections of the site - pulling in assets when required by each section. It does create the issue of a not very accurate progress state when preloading - assets within assets. But it has really made development easier and has made working with designers that much better. And with integration with CVS/SVN I don’t see myself developing in the Flash IDE any time soon!

    Shocks on November 23rd, 2007 at 11:20 am
  11. To access a symbol class in the embedded SWF I prefer using an instance of MovieClipLoaderAsset and a listener to an Event.INIT dispatched by Loader.contentLoaderInfo of its child. This solution based on Colin Moocks described in his EAS3, page 816-817.

    Your “MainFB” a little bit modified:

    package
    {
    import com.bigspaceship.test.*;
    import flash.display.MovieClip;
    import flash.events.Event;

    public class MainFB extends MovieClip
    {
    [Embed(source=’../bin/assets2.swf’)]
    private var AssetsClass : Class;
    private var _asset: MovieClipLoaderAsset;

    public function MainFB():void
    {
    _assets = new AssetsClass();
    Loader(_assets.getChildAt(0)).contentLoaderInfo.addEventListener(Event.INIT, _myInit);
    }

    private function _myInit($evt:Event):void
    {
    Loader(_asset.getChildAt(0)).contentLoaderInfo.removeEventListener(Event.INIT, _myInit);

    var b:BlueRectangle = new BlueRectangle();
    b.y = 200;
    addChild(b);
    }
    }
    }

    Anyway, thx for your great post!

    ;-)

    Best
    sectore

    http://www.websector.de/blog/

    sectore on November 24th, 2007 at 6:52 am
  12. Very nice post - it leads into the workflow Im trying to work out in as3 (via Eclipse/FDT3/Ant) using Ant and mxmlc/compc to compile my Main class to swf and using my flash assets compiled from flash ide - so that we can completely decouple the flash ide from the build process once raw assets are prepped.

    nokati on November 26th, 2007 at 11:41 am
  13. http://www.gskinner.com/blog/archives/2007/03/using_flash_sym.html

    Mr. Skinner has a very clean method to accomplish this also.

    Joel Hooks on December 7th, 2007 at 3:21 pm
  14. Just a short note on the Flex-only method for embedding. Just spent about 4 hours tearing my hair (which btw is non-existing) over a problem which seemed very, very strange to me. But which really was pretty basic when I found the solution.

    If you instantiate the AssetsClass into a local variable like in the example, and not are going to use any symbols from the swf until later, the assets could be garbage collected. After adding data loading to my constructor, and postponing creation of the graphics until this was done, this started happening about every third time I compiled the swf, resulting in runtime errors (null objects) when I tried to access assets.

    The solution, adding the assets variable as a class member instead of a local variable. Easy as pie. :)

    @ndre on January 14th, 2008 at 12:40 pm
  15. I don’t understand why Flex Builder 3 couldn’t give a simple way to include linkages directly from .FLAs. Asking people extra miles to work with Flash IDE / Flex Builder sort of pains.

    George on March 20th, 2008 at 7:27 pm