No matter what kind language using programmer will always accumulate many useful and daily using function with rich in development experience day by day. And then the accumulation become to a library at last. Of course it’s include the flex programmers. Following is some very basic, very simple functions which I’ve collected. How about you? If have any proudly and recommendable functions? Then, don’t only stock in your head, leave your comments. Let’s share.
No.1 Copy content to clipboard:
- System.setClipboard(strContent);
No.2 Clone an ArrayCollection:
- //dummy solution( well, it works )
- var bar:ArrayCollection = new ArrayCollection();
- for each ( var i:Object in ac ){
- bar.addItem( i );
- }
- // fantastic ! //
- var bar:ListCollectionView = new ListCollectionView( ListCollectionView( ac ).list );
No.3 Open URL:
- navigateToURL(new URLRequest('http://ntt.cc'), '_blank'
No.4 Page reload:
- navigateToURL(new URLRequest("javascript:location.reload();"),"_self")
No.5 Close browse:
- navigateToURL(new URLRequest("javascript:window.close()"),"_self");
No.6 Set the background alpha to transparent on Alert window:
- Alert
- {
- modalTransparency:0.0;
- modalTransparencyBlur:0;
- }
No.7 Set random color:
- lbl.setStyle('color', 0xffffff*Math.random());
No.8 Trim left of white space
- public function LTrim(s : String):String
- {
- var i : Number = 0;
- while(s.charCodeAt(i) == 32 || s.charCodeAt(i) == 13 || s.charCodeAt(i) == 10 || s.charCodeAt(i) == 9)
- {
- i++;
- }
- return s.substring(i,s.length);
- }
No.9 Trim right of white space
- public function RTrim(s : String):String
- {
- var i : Number = s.length - 1;
- while(s.charCodeAt(i) == 32 || s.charCodeAt(i) == 13 || s.charCodeAt(i) == 10 ||s.charCodeAt(i) == 9)
- {
- i--;
- }
- return s.substring(0,i+1);
- }
No.10 Trim left and right of white space
- public function Trim(s : String):String
- {
- return LTrim(RTrim(s));
- }
No.11 get data type:
- getQualifiedClassName(data)
No.12 Generate check digits
- private function GenerateCheckCode():String
- {
- //init
- var ran:Number;
- var number:Number;
- var code:String;
- var checkCode:String ="";
- //get 4 radom
- for(var i:int=0; i<4; i++)
- {
- ran=Math.random();
- number =Math.round(ran*10000); //get result like 0.1234
- if(number % 2 == 0)
- code = String.fromCharCode(48+(number % 10)); //0's ASCII code is 48
- else
- code = String.fromCharCode(65+(number % 26)) ; // A's ASCII code is 65
- checkCode += code;
- }
- return checkCode;
- }


April 20th, 2008 at 1:59 am
Two things about the ‘cloning’ of the ArrayCollection. First off, creating a ListCollectionView is generally a poor idea, as some functions specifically require an ArrayCollection. Secondly, this creates a new object, but it wraps the same array… You’ll get strange results when you add and remove objects to either object. This code will create a new object that is completely independent of the old.
var newArray:ArrayCollection = new ArrayCollection(oldAC.source.slice());
Also, for trimming strings, StringUtil.trim() works well… it trims both sides, but the code used there is very efficient, you can copy the start/end portions for your own use.
April 21st, 2008 at 9:05 am
Another option to clone an ArrayCollection is:
var newAC:ArrayCollection = new ArrayCollection(oldAC.toArray());
This will create a new ArrayCollection from an array that is populated in the same order as the old ArrayCollection.
Also, should number 11 be:
No.11 get *data* type:
April 21st, 2008 at 11:09 am
I’m surprised that the functions LTrim and RTrim are not already in the Flex framework.
April 22nd, 2008 at 12:28 am
Great idea of posting that list … kudos … after reading your post I was wondering if you or any other members of the AS/Flash/Flex community would be interested in a “smart” AS cheat sheet where we’d put all this usefull info.
Cheers
April 22nd, 2008 at 1:25 am
ltrim(), rtrim(), and trim() already exist in com.adobe.utils StringUtil as mentioned by the first commenter.
What reason is there not to use those functions? I can’t thing of one…
April 22nd, 2008 at 12:05 pm
I feel regular expression suits trimming function better.
April 22nd, 2008 at 2:11 pm
@Dusty Jewett ,Darren, Dallas Clark, eli
Thanks your comments.
Strickly to say I’m not a member of the AS/Flash/Flex community,
I’m just a C++ programmer and be used to write some basic function by myself. If you have interesting could see here for details:
http://ntt.cc/2008/02/12/flex-vs-ajax%E2%80%94both-have-their-pros-and-cons.html
I’ve mentioned before.
@ julien
I just want to share useful information for everyone sincerely. and there is nothing for others. But I think you are very “smart”,
April 22nd, 2008 at 10:40 pm
var newArray:ArrayCollection = new ArrayCollection(oldAC.source.slice());
var newAC:ArrayCollection = new ArrayCollection(oldAC.toArray());
The problem with both of these is they produce a shallow copy of the array. In the developer guide page 156 and 157 it talks about making a deep copy of arrays.
It goes like this:
import flash.utils.ByteArray;
function clone(source:Object):*
{
var myBA:ByteArray = new ByteArray();
myBA.writeObject(source);
myBA.position = 0;
return(myBA.readObject());
}
This will work for both indexed and associated arrays.
April 24th, 2008 at 9:59 pm
Simple but useful
——————————
toString(16)转化成16进制字符串。最大只能表达0×7FFFFFFF。表示RGB够用,但是表示ARGB (alpha, red, green, blue )不够。to hex string
toString(2)转换成2进制字符串。to binary string
——————————-
tip:parseInt可以转换字符串到int,第二个参数是基数。
// decimal:
trace(parseInt(”16711884″)); // 16711884
// hexadecimal:
trace(parseInt(”FF00CC”,16)); // 16711884
// binary:
trace(parseInt(”111111110000000011001100″,2)); // 16711884
April 27th, 2008 at 10:09 pm
Regarding #4 and #5, the use of “javascript:” in a URLRequest has been prohibited under the new 9.0.124 player security restrictions.