Creating a SWC Component in FlashDevelop

In the past, if you wanted to create a SWC library from your ActionScript classes you either had to use FlexBuilder or the Flash IDE. After Adobe open-sourced the Flex SDK you were also able to use the command line tool compc.exe at no extra cost. But if you don’t like to mess around with command line tools you have another option now. A developer from Sydney released a SWC export plugin for the popular open-source ActionScript development environment FlashDevelop. The plugin is based on compc.exe but it hides all the ugly details from you.

This tutorial will guide you through the steps of creating a simple ActionScript component and compilling it into a SWC file.

Installing the SWC Export Plugin

The plugin installation is a piece of cake. Download the plugin from here, unzip the ExportSWC.dll file and place it in FlashDevelop’s Plugin folder. (If you don’t know where to find this folder go to the “Tools” menu and click on “Application Files…”.) After restarting FlashDevelop you should see a new icon in the toolbar:

ExportSWC Icon

Sample Project: A Status Component

You will be creating a simple status component as a sample project. It displays different icons according to the status set. As for the icons, I’m using two icons from the free “Silk” icon set which can be found on www.famfamfam.com:

Sample Icons

The first icon is supposed to represent an “OK” status, and the other one signals an “Error” status. (For example, you could use this component as an item renderer in a Flex DataGrid for displaying the status of the listed items.)

Setting up the FlashDevelop Project

Create a new project in FlashDevelop and select “ActionScript 3 / Default Project” as template. Call the project “StatusComponent” and save it. If you look at the project panel you will notice that FlashDevelop has already created two folders (bin and src) and a class file (Main.as) for you. Please make sure that the src folder is the only folder in your classpaths settings:

Project Properties

Now follow these steps:

  1. Create a third folder on the same level as the bin and src folders and call it assets.
  2. Copy the two icon files into this new folder.
  3. Under the src directory create a hierarchy of three sub directories: com/log2e/components. (I’m using my domain name here, you can name them whatever you like, of course. But it’s considered good practice to organize your class packages with the help of unique names such as domain names.)
  4. Finally, move the class file Main.as into the components folder and rename the file to StatusComponent.as.

Your project panel should look like this now:

Project Panel

Now open the StatusComponet.as file.

The ActionScript Code

I’m posting the complete class code first, and then I will go through the code step by step.

  1. package com.log2e.components
  2. {
  3.    import flash.display.Sprite;
  4.    import flash.display.Bitmap;
  5.  
  6.    public class StatusComponent extends Sprite
  7.    {
  8.       public static const STATUS_UNSET:String = "status_unset";
  9.       public static const STATUS_OK:String = "status_ok";
  10.       public static const STATUS_ERROR:String = "status_error";
  11.  
  12.       [Embed(source="../../../../assets/accept.png")]
  13.       private static var iconOK:Class;
  14.       [Embed(source="../../../../assets/exclamation.png")]
  15.       private static var iconError:Class;
  16.  
  17.       private var _status:String;
  18.       private var _statusIcon:Bitmap;
  19.  
  20.       public function StatusComponent()
  21.       {
  22.          _status = STATUS_UNSET;
  23.       }
  24.  
  25.       public function get status():String
  26.       {
  27.          return _status;
  28.       }
  29.  
  30.       public function set status( status:String ):void
  31.       {
  32.          _status = status;
  33.  
  34.          if (_statusIcon)
  35.          {
  36.             removeChild(_statusIcon);
  37.          }
  38.  
  39.          switch(_status)
  40.          {
  41.             case STATUS_OK:
  42.                _statusIcon = new iconOK();
  43.                addChild(_statusIcon);
  44.                break;
  45.             case STATUS_ERROR:
  46.                _statusIcon = new iconError();
  47.                addChild(_statusIcon);
  48.                break;
  49.          }
  50.       }
  51.    }
  52.  
  53. }

Line 1: The package definition reflects the directory structure we created below the src folder.

Lines 3-4: We only need Sprite and Bitmap as display objects.

Line 6: Since we are creating a visual component the StatusComponent class extends Sprite.

Lines 8-10: The three constants hold the status definitions as strings. They are accessible from outside the component (for example, StatusComponent.STATUS_OK).

Lines 12-15: The two [Embed] metadata tags include the two icons in the SWF and SWC files at compile-time. Instances of the embedded assets can be created by the standard new syntax.

Lines 17-18: The two private variables hold the current status information and the displayed icon.

Lines 20-22: When a new instance of the component is created the constructor sets the initial status to unset.

Lines 25-28: The getter method for the status property.

Lines 30-50: The setter method for the status property. This method not only updates the private _status property, but also removes the currently displayed icon from the display list and instantiates a new icon according to the status set.

Exporting the SWC File

All the hard work is done. The SWC file is only a mouse click away. Of course, you can test the component first by hitting F5. In this case you should modify the constructor and set the initial value to STATUS_OK or STATUS_ERROR, otherwise you will only see a blank canvas when you publish the movie. For the release version of the component set the initial value back to STATUS_UNSET. Now click on the new “Export SWC” icon and the magic happens right in front of your eyes…

If everything went ok, you will see a new file in your bin folder:

bin Folder

If you tested the component (like I did) you also have the SWF file in the bin folder. Actually you can delete the StatusComponent.swf file now, it’s no longer needed.

Using the StatusComponent

For a quick test of the component create a new default ActionScript 3 project in FlashDevelop, call it “StatusComponentTest”, and make the StatusComponent.swc file accessible to your project by adding its path to the new project’s classpaths. The project panel should look like this:

Test Project Panel

Now this is important: Right-click on the StatusComponent.swc file and select “Add To Library”. Otherwise the class(es) in your SWC file will not be recognized.

Here’s a simple test class (StatusComponentTest.as) that instantiates the component and sets the status to STATUS_OK. The trace() statement sends the string value of the current status to the output panel.

  1. package
  2. {
  3.    import com.log2e.components.StatusComponent;
  4.    import flash.display.Sprite;
  5.    import flash.display.StageScaleMode;
  6.    import flash.display.StageAlign;
  7.    
  8.    public class StatusComponentTest extends Sprite
  9.    {
  10.       private var _statusComponent:StatusComponent;
  11.  
  12.       public function StatusComponentTest()
  13.       {
  14.          stage.scaleMode = StageScaleMode.NO_SCALE;
  15.          stage.align = StageAlign.TOP_LEFT;
  16.    
  17.          _statusComponent = new StatusComponent();
  18.          _statusComponent.status = StatusComponent.STATUS_OK;
  19.          addChild(_statusComponent);
  20.          trace(_statusComponent.status);
  21.       }
  22.    }
  23.  
  24. }

I hope you enjoyed the tutorial.

Tags: , , , ,

9 Responses to “Creating a SWC Component in FlashDevelop”

  1. John Giotta says:

    Whenever I try this, FlashDevelop crashes :(

  2. FlashDevelop crashes for me too, error:

    System.MissingMethodException: Método não encontrado: ‘System.String ProjectManager.Projects.AS3.MxmlcOptions.get_Additional()’.
    em ExportSWC.PluginMain.Export(Object sender, EventArgs e)
    em System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)

  3. Daniel try this:
    http://curlyben.com/fd3/ExportSWCPlugins.zip

    Thnx for the Tutorial. works like a charm. I am though trying to use the same exported swc in Flash CS3 with no luck. Adding the component on the stage adds nothing, while in FD works fine. Any ideas?

  4. Marcelo Stanger says:

    Hi… The tutorial is great,but with the new version of FD RC1 …there is any update chanse for this ?

    Thanks

  5. seany_on_craic says:

    For some reason, the “ExportSWC” button remains disabled (greyed out) no matter what I do…I am running FD 3.0 RC1…has anyone solved this? I don’t want to resort back to using the command window to get this done :(

  6. Andy says:

    My icon is also grey, i’m using FD3.0 RC2, could any one tell me why?

    Thanks greatly.
    Andy

  7. Ali (dragonworx) says:

    Hi, the ExportSWC project is now an opensource project hosted by SourceForge!

    http://www.sourceforge.net/projects/exportswc

    You can download the latest binary, or the SVN C# source if you would like to extend/modify the project. Just send your changes to the project admins and we’ll merge it. That way there is one location for all required features.

    Cheers.

  8. [...] post talks about how to create a SWC using flash develop and ExportSWC this approach is great if you have say a whole library of files that have no main.  [...]

Leave a Reply