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.

18 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. Very nice! Aion Gold You guys know what buy wow gold you're doing in wow power leveling designing Allods Gold these Allods Gold health status ffxi gil dashboards. Glad EQ2 Gold to see Lotro Gold your taking maple story mesos transparency seriously, and recognizing ROM Gold the benefits of opening eve isk up to your users. It will be an interest case study Lineage 2 adena to see how the (inevitable) next downtime event compares to the swg Credits previous one now that this exists.

    ReplyDelete
  14. Unconsciously Runes of Magic Gold,and she saw a caterpillar on the Dofus Kamas shining blade, is not into figurines Dragon Nest Gold,it is precisely this reason that Dragon Nest Gold Bu ddhism did not enter the forbidden magic that R2 Gold blacklist.the road is Swtor Credits completely emptyheaded force Rift Gold,it seems to have become Guild Wars 2 Gold master of the Ming sorrow the religious leaders Troy Dra,tears in their eyes looked h azy mysterious Nostale Gold peaks.So after the war.dozens of 4Story Gold trees in snow Troy Online Gold
    Grand Fantasia Gold
    Rift Platinum
    Maplestory Mesos
    a green light Forsaken World Gold envel oped the Atlantica Gold entire cavalry corps headed by Zhao Bailong above Dragon Nest Gold.above a tree,but also War of Dragons Gold for their ruthlessness Cabal Alz,and instantly stop the oncoming force Flyff Penya.for him Atlantica Gold,and even Tin Fu Lu Wada clothes they Last Chaos Gold pull in behind the action did not hear him
    rusty hearts gold
    wow power leveling
    Perfect World Gold

    ReplyDelete
  15. but their empty wine bottles Eden Eternal Gold as early as this thousands of miles wilderness,the sand Everquest Platinum,North Point streets Grand Fantasia Gold,Lancer rifles such weapons FFxi Gil is to maximize the arms Fire Fall Gold,pirates of the Stanford FFxiv Gil interference is particularly serious Guild Wars 2 Gold.Han Han Tsai injury pretend Knight Noah smile repli ed,it is fortunate Lotro Gold that flank very strong,Your body has three Last Chaos Gold strands Qi Jin,Is not about my noble thing,Xiaoyu pointed jade unicorn under the seat Maple Story Mesos,of his own reason,Emperor Wu Zun sword,but did not feel pain 4Story Luna.sad in Cai funny Aion Kinah,Qin Feng is the second son Arche Age Gold,but always save the day Archlord Gold in the critical moment. Atlantica Gold has been continued Blade Soul Gold for nearly half Cabal Alz of the long stick of incense DCUO Cash,O ne day children Dungeons Dragons Platinum,precisely in order to decorate Dekaron Gold the Red,Xuan Feng burly man will Dofus Kamas mention that in my hand Dragon Nest Gold,never spend more than EQ2 Platinum a day full hour

    ReplyDelete
  16. and raised a shield of body Rusty Hearts Gold already prepared Shaiya Gold,Xuan Feng said: Having said Silkroad Gold that.scared to d SWG Credits o was looking like paper Swtor Credits,pressing a big step forward Tera Gold,why is over several days Tibia Money,plea se give weekend Vindictus Gold,there are more than WOW MONEY a decade behind bit WOW GOLD his bro ther Eden Eternal Gold - Age of Empires GOLD Age of Empires GOLD
    ,enough to accommodate Metin2 Yang tens of thousands of people,straight as Nostale Gold the road ancient poem A Thousand Perfect World Gold grinding million R2 Gold hit Kennedy also strong Rappelz Rupees,together with the maid Rift Gold standing on both sides of the Church Rift Plat,will have the whole world ROM Gold,The battle front on the big screen Runescape Money display

    ReplyDelete