Deprecated: Assigning the return value of new by reference is deprecated in /nfs/c02/h01/mnt/46561/domains/kreativeking.com/html/wp-settings.php on line 520

Deprecated: Assigning the return value of new by reference is deprecated in /nfs/c02/h01/mnt/46561/domains/kreativeking.com/html/wp-settings.php on line 535

Deprecated: Assigning the return value of new by reference is deprecated in /nfs/c02/h01/mnt/46561/domains/kreativeking.com/html/wp-settings.php on line 542

Deprecated: Assigning the return value of new by reference is deprecated in /nfs/c02/h01/mnt/46561/domains/kreativeking.com/html/wp-settings.php on line 578

Deprecated: Function set_magic_quotes_runtime() is deprecated in /nfs/c02/h01/mnt/46561/domains/kreativeking.com/html/wp-settings.php on line 18
Cool Flash Tip of the Week #3 | kreativeKING - Interactive Developer

' 09 June, 10 - 2:41 pm

Cool Flash Tip of the Week 3

Post Header

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.<DataType>
 
//Correct
var myVector:Vector.<Sprite> = new Vector.<Sprite>();
 
//Throws Error
var myVector:Vector.<DisplayObject> = new Vector.<Sprite>();


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.<Sprite> = new Vector.<Sprite>();
myVector[0] = new Sprite();
var myVector2:Vector.<DisplayObject> = Vector.<DisplayObject>(myVector);
 
//Copy an Array to a Vector
var myArray:Array = [new Sprite(), new Sprite()];
var myVector2:Vector.<DisplayObject> = Vector.<DisplayObject>(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.<DisplayObject> = Vector.<DisplayObject>(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.<DisplayObject> = Vector.<DisplayObject>(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.<DisplayObject> = new Vector.<DisplayObject>(3, true);
trace(myVector2) //outputs null,null,null
 
var myVector2:Vector.<DisplayObject> = new Vector.<DisplayObject>(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.


More Info

No related posts.

  • It was quite easy to learn vector, this might be because I am aware of array. Now would love to use vector in more detailed way, once basics are known, we can learn this by own also.

  • Good topic

  • Hi, Everything dynamic and very positively! :)

  • Samuel Asher Rivello

    This topic looks great.

  • Hey, great post, very well written. You should post more about this.

blog comments powered by Disqus