In a recent Flash animation that I updated featuring various mouse cursors, I used an associative array to represent each cursor.
You can view the Flash here at Newgrounds: Cursorize
What an associative array allows you to do, is create key-values pairs where you can create your own array indices (keys)and assign them the appropriate value. Associative arrays differ from traditional arrays in that associative arrays allow you to index an array with something other than an integer. Normal arrays use integers starting at zero to index them. This is completely fine, but sometimes you want an index with a more descriptive and meaningful name than just 0, 1, 2, and 3…and so on.
A normal array declaration in Flash Actionscript is as follows:
var array:Array = new array();
This creates an array with no predetermined length, hences the blank argument when creating the array object.
This type of array has index/element pairs as follows:
array[0] = “zero”;
array[1] = “one”;
array[2] = “two”;
etc…
In my Cursorize Flash, I used an associative array:
var cursors:Array = [{link:"square"}, {link:"triangle"}, {link:"shuriken"}, {link:"star"}, {link:"windmill"}, {link:"sword"}, {link:"line"}];
This associative array looks much different than the traditional array. Basically what this says is that the first index in this array has a key called “link” whose value is “square”. Now we have a way of refering to the different cursors through a meaningful name, “link”, instead of just index 0, 1, and 2. This becomes extremely helpful and useful if you have complex projects that require well-defined object/variable names.
Here is how I made use of the associate array. We still need to use the integer index like before because square still has an index of 0 and triangle an index of 1.
Here is a function that uses the associative array:
function get_style()
{
var current_cursor:String = cursors[tracker].link;
var first_char:String = current_cursor.substr(0,1);
first_char = first_char.toUpperCase();
var rest_char:String = current_cursor.substring(1);
var style:String = first_char + rest_char;
return style;
}
I have a global variable called tracker, which keeps track of the current cursor being used. Tracker is actually the index of the key-value pairs in the associative array.
var current_cursor:String = cursors[tracker].link;
This means retrieve the value in the ”cursors” associative array for the key whose value is “link” and index is “tracker”.
So for the square, if tracker is zero:
var current_cursor:String = cursors[0].link;
This would return the string “square” and store in the variable “current_cursor”. This “square” value is then mapped to the linkage ID of the movieclip in the library.
Here is a screenshot of what the linkage ID in Flash looks like:
Another great reason to use associative arrays is that you can create several key-value pairs per index, as many as you need. You just need to make sure that you are referring to them using the correct syntax. With normal arrays, you only have a single index mapping to a single value. In a way, associate arrays are similar to XML tags where in XML you can have several attributes with a different values.
If I want to add more cursors to this Flash, all that I would have to do is add the new movieclip cursor to the associative array and that is it. So using arrays really makes the Flash extensible and maintainable. This is a key goal when designing the code.
In short, associative arrays allows you to create descriptive names for your keys and it allows multiple key-value pairs for each index. Hopefully this has helped you understand more about the applications for associative arrays in your coding projects.

WordPress Blog Feed
0 Responses to “Codetorial: Associative versus Traditional arrays in Flash Actionscript (Cursorize)”