AS: Arrays
Arrays are a great way to hold, access and manipulate variables, especially in variable-heavy games such as RPG & Sims.
Array Creation
You can either create an empty one:
_root.holdarray=new Array();
or specify values as you create it:
_root.holdarray=new Array("D", "E", "F");
-------------------------------------------------
Reading Values
Assuming you've used the second method to create your array:
If you wish to check the contents of an array, use
trace(_root.holdarray) //(displays: D,E,F)
If you want to find the number of values in an array, use
trace(_root.holdarray.length) //(displays: 3)
If you want to access a particular value:
trace(_root.holdarray[0]) //(displays: D)
Note that the first value in an array is [0], so in this example, _root.holdarray[0] is D, _root.holdarray[1] is E, and _root.holdarray[2] is F
-------------------------------------------------
Changing Values
To change a value, simply restate it:
_root.holdarray[1]="Z"; //(trace(_root.holdarray) will display: D,Z,F)
-------------------------------------------------
Adding Values
You have three options when adding a new value to an array
To add it at the END of the array:
_root.holdarray.push("G"); //(trace(_root.holdarray) will display: D,E,F,G)
To add a value at the START of the array:
_root.holdarray.unshift("C"); //(trace(_root.holdarray) will display: C,D,E,F)
To add a value in the MIDDLE of an array, you need to specify your insertion point and a delete count.
This is a little more complex, and follows this format: array.splice(Insertion point, Delete Count, Value to insert)
If you don't wish to delete a value when splicing, just use '0' for the Delete Count
_root.holdarray.splice(2, 0, "K"); //(trace(_root.holdarray) will display: D,E,K,F)
_root.holdarray.splice(1, 1, "K"); //(trace(_root.holdarray) will display: D,K,F)
-------------------------------------------------
Removing Values
To remove the FIRST value in an array:
_root.holdarray.shift(); //(trace(_root.holdarray) will display: E,F)
To remove the LAST value in an array:
_root.holdarray.pop(); //(trace(_root.holdarray) will display: D,E)
To remove a value in the MIDDLE of an array, use array.splice (mentioned above), but don't specify a value to insert this time:
_root.holdarray.splice(1, 1); //(trace(_root.holdarray) will display: D,F)
-------------------------------------------------
Manipulating Arrays
To reverse the order of values in an array:
_root.holdarray.reverse(); //(trace(_root.holdarray) will display: F,E,D)
To sort the contents of an array alphabetically or numerically, you can use array.sort() or array.sortOn.
There are various optional parameters you can use when sorting an array... since I'm trying to keep this post reasonable simple, I'll refer you to the official pages: sort - sortOn
-------------------------------------------------
There are a couple of array functions I haven't covered (concat and toString). For more information on arrays, check out these links:
Array information
http://www.macromedia.com/supp...ctionscript_dictionary059.html
http://www.communitymx.com/content/article.cfm?cid=AD782
http://www.macromedia.com/devnet/mx/flash/extreme/extreme006.html
http://www.actionscript.org/tutorials/intermediate/Arrays/index.shtml
===================================
AS: Sections
AS: Preloader
AS: Sound
AS: Basic Movement
AS: Collision Detection by BleeBlap
AS: Random Movement by Begoner
AS: Movement on slopes by Joelasticot
AS: Save and Load
AS: Clock by Glaiel_Gamer
AS: Pong physics & gravity by Dark_Toaster
AS: API by -liam-