Friday 24 February 2012

Actionscript 3- Vector to Array

There’s currently no way to convert a Vector to an Array (or ArrayCollection), so here’s a simple util that’ll do it. Accepts any type within the Vector, so the parameter is just an Object, then we convert it back to a vector before the Array conversion. Phew.
  1. public static function VectorToArray( v:Object ):Array  
  2. {  
  3.     var vec : Vector.<object> = Vector.<object>(v);  
  4.     var arr : Array = new Array()  
  5.     for each( var i : Object in vec ) {  
  6.         arr.push(i);  
  7.     }  
  8.     return arr;  
  9. }  
  10. </object></object>  
Post to Twitter

2 Responses to “Actionscript 3- Vector to Array”

Daniel Bunte says:
Hi there!
Just found this post. Am I allowed to clean and speed it up a bit?
public static function VectorToArray( v:Object ):Array
{
var vec : Vector. = v as Vector.,
arr : Array = [];
for each( var i : Object in vec )
{
arr[arr.length] = i;
}
return arr;
}
Cheers,
Daniel

No comments:

Post a Comment