Welcome back everyone for the 3rd installment of the Cool Flash Tip of the Week Series. This week we are going to take a look at a new class introduced with Flash Player 10. Say hello to the Vector Class.
The Vector class is very similar to the Array class which we all know and love. The difference is that a Vector only holds one data type. An Array can holds several different kinds of values. A Vector is an Array of the same type. Check out the psuedo code underneath to get a better understanding of what I mean.
//Array
[ String, Number, ,int, Sprite ];
//Vector
[ String, String, null, String, String ];
If you paid close attention to that last snippet, you notice that the Array has an empty index, which is just fine. But when it comes to a Vector, there can not be any empty indices. If you must have an empty spot, make sure to set the value to null
. Leaving it empty will throw an Error. A Vector is a dense data typed Array. Moving right along, to create a Vector is quite different than creating any other instance in Actionscript 3. Check out the example below.
//Syntax
Vector.
//Correct
var myVector:Vector. = new Vector.();
//Throws Error
var myVector:Vector. = new Vector.();
As you can see, the type of the Vector can only be instantiated to that class. Even though Sprite is a subclass of DisplayObject. So make sure to avoid this and you should have a smooth time. There is however a way we can still make this work. This would be using the Vector()
function. This will allow you to turn an Array or Vector into another base class.
//Copy a Sprite Vector to a DisplayObject Vector
var myVector:Vector. = new Vector.();
myVector[0] = new Sprite();
var myVector2:Vector. = Vector.(myVector);
//Copy an Array to a Vector
var myArray:Array = [new Sprite(), new Sprite()];
var myVector2:Vector. = Vector.(myArray);
//Make sure your Array are all the same Type or you will get an Error
var myArray:Array = [new Sprite(), new Sprite(), new String()];
var myVector2:Vector. = Vector.(myArray); //Throws Error because String is not a DisplayObject
//They can all be DisplayObjects though
var myArray:Array = [new Sprite(), new Sprite(), new MovieClip()];
var myVector2:Vector. = Vector.(myArray); // Accepts this array since Sprite and MovieClips are both DisplayObjects
So based on our examples, directly assigning a SubClass Vector to a SuperClass Vector will throw an Error. You must copy the Array or Vector into the the Super Class Vector. The Vector constructor takes 2 optional parameters. The first parameter is the length and the second is the fixed parameter. If you set the fixed parameter to true, trying to create something in an index past that will throw an Error. This result is the same when using a method that changes the length of the Vector such as pop()
, push()
, etc.
var myVector2:Vector. = new Vector.(3, true);
trace(myVector2) //outputs null,null,null
var myVector2:Vector. = new Vector.(3, true);
myVector2[3] = new Sprite(); // Throws Error because fixed is set to true and we tried to place something in an index that exceeds the length.
Now with all these restrictions, this makes Vector a lot faster than using Arrays and is recommended when dealing with an Array which holds all the same DataType. Vectors are approximately 40% faster according to Mike Chambers to Arrays. When dealing with a large Array that is the same type, use a Vector. Also note that Vectors are only available in Flash Player 10, so that is bit of a downfall for the moment, but will be the new standard once the Flash Player 10 penetration numbers get higher.
Hopefully this little look into Vectors will get you to make the switch if you are publishing to the 10th version of the Flash Player. Next week, be prepared, we Are going to take a look into a Custom Class. If you enjoyed this post, tweet it, share it, bookmark it and subscribe. Stay tuned for next week.
Nice stuff man, I'm gonna follow you on Twitter as soon as I figure out what my stupid password is... Gzzz
ReplyDelete[...] DIRECT LINK » [...]
ReplyDelete[...] Welcome back everyone for the 3rd installment of the Cool Flash Tip of the Week Series. This week we are going to take a look at a new class introduced with Flash Player 10. Say hello to the Vector Class. DIRECT LINK » [...]
ReplyDeletequote:
ReplyDelete"...but will be the new standard once the Flash Player 1 penetration numbers get higher..."
small typo - I do believe you mean't:
"... Flash Player 10 ..." and not "1" yes? :)
Ha, Thanks for catching that.
ReplyDeleteThanks for the useful info. It’s so interesting
ReplyDeleteHey, nice post, really well written. You should blog more about this. I'll definitely be subscribing.
ReplyDeleteHey, great post, very well written. You should post more about this.
ReplyDeleteThis topic looks great.
ReplyDeleteHi, Everything dynamic and very positively! :)
ReplyDeleteGood topic
ReplyDelete[...] Cool Flash Tip of the Week #3 | kreativeKING – Interactive Developer [...]
ReplyDelete