Monday, February 23, 2009

Using StringUtil Class in Flash

Using Flex ActionScript 3.0 classes in Flash is now easier than ever before due to the new addition of the Flash Library path and external library path.
Am going to demonstrate how users can use StringUtil class without the need to go through the hoops to make it work in Flash.


1. Launch Flex Builder.
2. Create "Flex Library Project" and call it "FlexUtilFlash".
3. Right Click on the "src" folder and create "ActionScript Class" and call it "FlexUtilFlash.as".
4. Copy and paste the following code to the class

package
{
import mx.utils.StringUtil;

public class FlexUtilFlash
{
public function FlexUtilFlash()
{
new StringUtil();
}

}
}

In the above class all what am doing is instantiating StringUtil class and include it into my SWC that am going to use in Flash.

5. Save.

Note : FlexUtilFlash.swc file automatically created and saved in the bin directory of the project file after saving the FlexUtilFlash.as file as below :



6. Launch Flash CS4 Professional.
7. Choose File > New > Flash File (ActionScript 3.0).
8. Choose File > Save and name the file FlashFlexUtil.fla. The location of the saved Flash file is unimportant, so you can save it in any folder you like.
9. Choose File > Publish Settings to access the Publish Settings dialog box.
10. Click the Flash tab to see the Flash Player and ActionScript settings.
11. Click the Settings button next to the ActionScript drop-down menu.
12. Click the Library Path tab.
13. Click the red SWC icon and browse to select the SWC file named FlexUtilFlash.swc that was generated by Flex in Step 5. After you select the FlexUtilFlash.swc file, the path to the file is added to the Library path window as below :



14. Open the Actions panel by choosing Window > Actions. Since the FlashFlexUtil.fla file contains only one layer, Frame 1 of Layer 1 is automatically selected. Copy and paste the following code into the Script window to instantiate an object of FlashFlexUtil:

import mx.utils.StringUtil;
var str:String = " This is Awesome!!!";
trace(str);
var sttTrim:String = StringUtil.trim(str);
trace(sttTrim);
15.Test Movie and you should see the result should be as below :





As you see above, adding the SWC allow you to use all the StringUtil class methods and properties in Flash.

20 comments:

  1. Thnx, this is gr8. Flex SDK has so many additional APIs that are missing in Flash actionscript. Now, we can utilize those. I wonder, whether we can use ArrayCollection in Flash. I'll try to check and see.

    ReplyDelete
  2. I followed the same steps to utilize ArrayCollection class in Flash. It's giving me an error - "Error: No class registered for interface 'mx.resources::IResourceManager'." Can you plz share which Flex classes can be used and which ones can't be used in Flash. Thnx!

    ReplyDelete
  3. It should work. Any pure Flex Actionscript class that does not rely on the Flex Framework should work in Flash.
    Try this
    create Flex Library project and copy and paste the following code into Actionscript class

    package
    {
    import mx.collections.ArrayCollection;

    public class ArrayCollectionFlash
    {
    public function ArrayCollectionFlash()
    {
    /**
    *
    */
    new ArrayCollection();
    }

    }
    }

    after you save and generate the SWC then add it to the Flash library path and then copy and paste the following code to the action panel

    import mx.collections.ArrayCollection;

    var arr:ArrayCollection = new ArrayCollection([1,2,3,4]);

    trace(arr.source);



    Now Test movie.

    Thanks,
    Tareq

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. It sounds like you are using an older version of the Flex SDK that had a bug where it required even ActionScript-only projects to have a ResourceManager. You can either update to the latest Flex SDK or use the workaround mentioned in the bug report - https://bugs.adobe.com/jira/browse/SDK-12205.

    HTH,
    Nivesh

    ReplyDelete
  6. Tareq --

    I believe that your otherwise-excellent article, "Code in Flex, test in Flash" (http://www.adobe.com/devnet/flash/articles/flex_to_flash.html, published 16 February 2009) contains the following error:

    The caption to Figure 3 is:
    - "Setting the source path information in Flash CS4."
    ...when it should instead be
    - "Setting the source path information for a Flex Library Project in Flex Builder 3."

    Please let me know if this proposed correction is itself in error. If not, I encourage you to revise the published article accordingly.

    Hoping that my own published errors are as trivial as this one, I am

    Yours Respectfully,

    Jim Plamondon
    www.igetitmusic.com/blog
    Austin, Texas

    P.S.: I am unable to access, either in Flash CS4 or Flex Builder 3, a screen that looks like Figure 7 in the above-mentioned article. What is the path to this screen?

    ReplyDelete
  7. Hi Jim,
    Good catch :). it was indeed a mistake and it is fixed.
    Thanks for pointing that out.

    Regarding your question on Figure 7, you can access it in Flash by doing the following :
    1. Edit > Preferences
    2. Select "Actionscript" category.
    3. Click on "Actionscript 3.0 Settings" button.

    Thanks again,
    Tareq

    ReplyDelete
  8. I tried to get this to work with the String.restrict() method from Flex Gumbo but it wouldn't. But String.trim() worked just fine.

    What could I be doing wrong or what should I be doing?

    ReplyDelete
  9. Nevermind: I found out what I was doing "wrong": I wasn't using the right version of Flex Gumbo. To get the StringUtil.restrict method, you will have to download the following Flex Gumbo SDK build:

    http://opensource.adobe.com/wiki/display/flexsdk/download?build=4.0.0.4904&pkgtype=1

    The StringUtil.restrict method is analogous to the TextField.restrct method, except it removes unwanted characters from a string using similar syntax!:http://livedocs.adobe.com/flex/gumbo/langref/mx/utils/StringUtil.html#restrict()

    THANK YOU VERY MUCH FOR THIS GEM!!!

    ReplyDelete
  10. Great!

    You could have also checked the StringUtil class documentation for that specific SDK to see if the method is there or not.

    Thanks,
    Tareq

    ReplyDelete
  11. Thanks for this post. But I have still getting another error:
    1061: Call to possibly undefined method addItem through a reference with static type ArrayCollection.

    Can anione help me?

    ReplyDelete
  12. Download Flex SDK 4.0.0 (http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4)
    and add the framework.swc from SDK4 to your fla library path and try again.
    that should do it.

    thanks,
    tareq

    ReplyDelete
  13. thanks for the latest updates..
    shiningweb

    ReplyDelete