Flash Actionscript string[] array
August 22, 2006
Someone found my blog searching for “flash actionscript string[] array”.
I think this person is looking for a way to build a string array in ActionScript. That’s easy.
The ActionScript language reference explains the Array constructor like this:
public Array([value:Object])
Let’s you create an array. You can use the constructor to create different types of arrays: an
empty array, an array with a specific length but whose elements have undefined values, or an
array whose elements have specific values.
- Usage 1: If you don’t specify any parameters, an array with a length of 0 is created.
- Usage 2: If you specify only a length, an array is created with length number of elements. The value of each element is set to undefined.
- Usage 3: If you use the element parameters to specify values, an array is created with specific values.
Parameters
value:Object [optional] – Either:
- An integer that specifies the number of elements in the array.
- A list of two or more arbitrary values. The values can be of type Boolean, Number, String,Object, or Array. The first element in an array always has an index or position of 0.
So here are two examples of how you could go about creating a String array in Flash
var str_example_array:Array = new Array(”Belinda”, “Gina”, “Kathy”, “Charlotte”,”Jane”);
Or
var str_example_array:Array = new Array();
str_example_array[0] = “Belinda”;
str_example_array[1] = “Gina”;
str_example_array[2] = “Kathy”;
str_example_array[3] = “Charlotte”;
str_example_array[4] = “Jane”;
I hope that this is of help. If you have questions, ask them!


September 15, 2006 at 4:14 pm
okay, so as far as i can tell, there is no conceivable way to specify a class (or even a primitive data type) for the elements in an array.
um… wtf?
please tell me i’m just missing something.
September 15, 2006 at 6:55 pm
Hi carolyn,
I think that’s a valid conclusion. Well at least for ActionScript up to version 2, I don’t know how things are arranged in ActionScript 3. Before ActionScript 2 it was even possible to switch the data type of a variable at runtime like this:
var alpha = 521;
alpha = “Bloomfield Rocks!”;
It’s called weak data typing, you don’t explicitly state data types and they are dynamicly changed by the operations you perform on them.
For example:
var alpha = 2;
alpha += ” be or not 2 be”;
Assigning 2 to alpha makes it a Number. The String concatenation operator ‘+=’ transforms alpha into a String.
As of ActionScript 2 this is no longer possible. Variables are now strong typed, in the sense that they keep the data type with which they where initialized. But explicitly stating the type is not obligatory.
I try to give my code as much type information as possible because I’ve heard that this increases the performance of the compiled code, but I never found a way to specify the type of Array content.
I do know how to specify types of variables in variable declarations with the ‘:’ operator .
For example:
var s:String = “Hi”;
And in function declarations.
function myFunction(param1:Number, param2:Object):String
{
return “result = “+(param1+param2.num);
}
Where parameters 1 and 2 (param1 and param2) have a explicit type and the function as a whole has a return type.
Besides making your code faster, it makes your code more readible and makes the compiler able to detect errorenous type usage. (For example trying to add a String return type from a function to a number.)
I wish I knew how to explicitly state the type content of an Array. For now I state it in my code comments. Please let me know when you found a solution. But I think it doesn’t exists. One strong indication of this is the fact that it’s possible to store different types in the same array. This code, I just tried, compiles without errors!
var i:Number = 9;
var o:Object = new Object();
var s:String = “Hello World!”;
var arr:Array = new Array(i,o,s);
September 15, 2006 at 10:22 pm
yeah.
when it’s been vital to ensure the type consistency of an array, i’ve just kept some quick little functions around to test each element to see if it matches the intended type and return a log of any that are an incorrect type… but that gets to be more of a pain if your array is full of objects with multiple kinds of data in each one. arg.
January 24, 2007 at 4:03 pm
I have a project that requires an 8×8 dot matrix array and I will have up to 20 or so of these. do you have some insight you could share on how to set up a structure that I can take binary data that would come from a lookup table like a case/switch to make up the on and off’s for the dots and with the push of a button shift in new data one column at a time? So what I have is an 8×8_0_mc prefix in each new _mc there will be instances of a dot I could controlled what color I wont the dot physically by adding frames or control each new instances by tint. ? if you could help or lead me in the right direction I would appreciate it
-Terrence
September 6, 2007 at 2:39 pm
hei,, i dont know if you have the time to answer me this questions, but i’m a little bit deseperated….
i have this script:
var arreglo3:String =_root.btn3admintitle;
var arreglo4:String =_root.btn4minsidetitle;
var arreglo5:String =_root.btn5systemtitle;
if(arreglo3 == “falso”){
_root.btn3.titu.text = arreglo3;
}
else{
_root.mainMenu.peliMovie.attachMovie(”btn3admin”, “btn3adminvideo”, this.getNextHighestDepth(), {_x:5, _y:18});
}
if(arreglo4 == “falso”){
this.btn4.titu.text = arreglo4;
}
else{
this.mainMenu.peliMovie.attachMovie(”btn4minside”, “btn4minsidevideo”, this.getNextHighestDepth(), {_x:5, _y:54});
}
if(arreglo5 == “falso”){
this.btn5.titu.text = arreglo5;
}
else{
this.mainMenu.peliMovie.attachMovie(”btn5system”, “btn5systemvideo”, this.getNextHighestDepth(), {_x:5, _y:92});
}
what i want to do is to evaluate several times different variables, wheter if they are = to “falso” or to ather string,
The problem is that when the script runs it only read the last script… the last “if”… how can i make it to read all the 3 “if’s”…?? or more?….
thanks
alejandro
September 6, 2007 at 3:01 pm
I don’t see anything wrong with this code that would prevent the if statements from being evaluated. Are you sure that the strings arreglo3, arreglo4 and arreglo5 contain the right data?
Try adding this statement right after you initialized the strings arreglo3, arreglo4 and arreglo5;
trace(”arreglo3: “+arreglo3+” arreglo4: “+arreglo4+” arreglo5: “+arreglo5);
Does this statement print out what you expected it to print out?