Wednesday, November 4, 2009

Convert a String into an Array Using Actionscript 3

I was trying to pass an array through SWFObject flashvars and realized it can't be done. Instead you need to use a string for your array, pass it into Flash , then convert it to an array using the actionscript "split" function.

//the string to turn into an array
var arrayString:String="1,2,3";

//convert the string to an array using the "," as a divider (aka delimiter)
var newArray:Array= arrayString.split(",")

//convert the array items from strings to numbers
var n=newArray.length;
while (n--) {
newArray[n]=Number(newArray[n])
}

//test: to make sure conversion to numbers worked
trace(String(trainCubesArray[0]+1)); //test: result is 2

No comments: