00:00
00:00

A S: Arrays 2005-06-28 06:13:50


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: Main

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-


- - Flash - Music - Images - -

BBS Signature

Response to A S: Arrays 2005-06-28 06:19:42


As usual, comments/revisions/suggestions/examples are appreciated, so anybody who has used arrays and has picked up tips and tricks for manipulating and using them, feel free to post.


- - Flash - Music - Images - -

BBS Signature

Response to A S: Arrays 2005-06-28 06:26:27


Probably the most useful one yet. I always wondered exactly how splice worked...

Response to A S: Arrays 2005-06-28 06:41:23


Another good one Denvish, I found the splice command perticularly interesting.

Response to A S: Arrays 2005-06-28 06:43:20


We could use an AS:Functions next, maybe I'll think about making one.

Response to A S: Arrays 2005-06-28 08:38:29


Thanks! Splice was what I was trying to figure out how to do (personal highscore board)

Response to A S: Arrays 2005-06-28 13:29:35


Oh, then...

Here are some custom methods for arrays than can be useful. If you haven't worked with object prototypes before, just have in mind to put this code anywhere prior to the frame/mc... where they are used.

Array.has(Element)
Description: returns a Boolean value indicating if array has or not the Element specified.
Code:

Array.prototype.has=function(Element){var have=false; for (var i=0; i<this.length; i++){have=have ||(Element==this[i]);}; return have;};

Please note that it doesn't check for strict equality and only the first level of elements.

Usage:
myArray=new Array(2,3,4);
trace(myArray.has(2)); //returns true

Array.onlyUnique()
Removes duplicate elements in an array.

Array.prototype.onlyUnique=function(){for(var i=(this.length-1); i>0; i--){for(var j=i-1; j>-1; j--){this[i]==this[j] ? this.splice(i,1) :0;};};};

Usage:
myArray=new Array(2,2,3,4);
myArray.onlyUnique(); //myArray is now== [2,3,4]

Array.prototype.toUpperCase

Returns an array with the contents of some other array in string and uppercased, without modifying the original array.

Code:
Array.prototype.toUpperCase=function(){var Caps=new Array(); for (var i=0; i<this.length; i++){Caps.push(this[i].toUpperCase());}; return Caps;};

Usage:
myArray=new Array("hello","folks");
trace(myArray.toUpperCase()); //returns "HELLO,FOLKS"

Response to A S: Arrays 2005-06-28 13:57:52


Array nesting

Nested arrays are just like normal arrays, but they contain other subsidiary arrays rather than numeric or text variables. They are sometimes refered to as multidimensional arrays.

myMap = new Array(
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 1, 1, 0],
[0, 1, 1, 0, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0],
[0, 1, 0, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0]);

This kind of array could be used to store a tile map for an RPG, just have a code in the tile that refers to the array and displays the right tile depending on your position. To get a value from a nested array is easy, you just need to use extra square brackets:

tile.gotoAndStop(myMap[y_co][x_co]+1);

The value in the first set of square brackets direct the script to an element within the 'myMap' array, if it were a numeric or text variable, that would be enough, but as it is another array we would need another set of brackets to reach something usable, so the second set of brackets direct a script to an element in the secondary array specified by the first set of brackets.

In the example, 'y_co' and 'x_co' variables represent the player's map co-ordinates, so if it were to be applied to a tile MC with different terrain graphics on each of it's frames, this code would make sure that the tile the player is standing on displays the correct terrain type.

You can have as many nested arrays as you need, I've never gone above 3 levels of nesting because It can get confusing and hard to manage, but it's useful for tile maps.

Response to A S: Arrays 2005-07-30 14:00:08


I've read the whole tutorial but it doesn't really teach how to use them.
For example using arrays for inventory, how can I do that?
Is it something like this?

inventory = new Array("sword","knife","potion","dagger","b
ow","arrows");

And then use them like variables?


BBS Signature

Response to A S: Arrays 2005-07-30 14:07:08


At 7/30/05 02:00 PM, -Toast- wrote: And then use them like variables?

You can't use the things you said as variables.. but to use variables in arrays just do this sort of thing:

_root.array = new Array(lol=1, lmao=2, rofl=3);
trace(_root.array[2]);

It will trace what the 2nd (3rd really, as the lol variable will be counted as 0) variable.


Sup, bitches :)

BBS Signature

Response to A S: Arrays 2005-07-30 14:15:57


At 7/30/05 02:07 PM, -liam- wrote:
At 7/30/05 02:00 PM, -Toast- wrote: And then use them like variables?
You can't use the things you said as variables.. but to use variables in arrays just do this sort of thing:

_root.array = new Array(lol=1, lmao=2, rofl=3);
trace(_root.array[2]);

It will trace what the 2nd (3rd really, as the lol variable will be counted as 0) variable.

Well, for an inventory, I usally use loads of variables but it sucks :P
Example:
sword = true;
axe = false;
bow = true;
arrows = 10;
etc...


BBS Signature

Response to A S: Arrays 2005-07-30 14:28:05


At 7/30/05 02:15 PM, -Toast- wrote: Well, for an inventory, I usally use loads of variables but it sucks :P
Example:
sword = true;
axe = false;
bow = true;
arrows = 10;
etc...

You can still do that, like this:

inventoryArray=new Array(sword=true,axe=false,bow=true,arrows
=10);
if(inventoryArray[1]==true){
//whatever
}


Sup, bitches :)

BBS Signature

Response to A S: Arrays 2005-07-30 14:31:29


Hmm.. Thanks, I will try that.

kthxbai :P


BBS Signature

Response to A S: Arrays 2005-07-30 14:41:41


At 7/30/05 02:31 PM, -Toast- wrote: Hmm.. Thanks, I will try that.

Yeah, might be easier for you to keep it out of an array :P

kthxbai :P

Kbai


Sup, bitches :)

BBS Signature

Response to A S: Arrays 2005-09-01 19:59:10


At 6/28/05 01:29 PM, ZYX3D wrote: lots of stuff

wow, thats some useful stuff right there! thatll be really helpful. i shall bookmarketh this page...eth.


snyggys

Response to A S: Arrays 2005-09-01 20:45:08


you can also make object arrays, and with that array you can run a for loop and make each object in that array run a custom function

customObj = function(inVal){
this.val=inVal;
this.change=function(){
this.val+=random(3);
trace(this.val);
}
}

objAry = new Array();

objAry.push(new customObj(3));
objAry.push(new customObj(5));
objAry.push(new customObj(10));

for(i=0;i<objAry.length;i++)
{
objAry[i].change();
}


BBS Signature

Response to A S: Arrays 2005-11-17 14:22:56


At 6/28/05 01:57 PM, JafitMan wrote: Array nesting
myMap = new Array(
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 1, 1, 0],
[0, 1, 1, 0, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0],
[0, 1, 0, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0]);

can someone do a more indepth explanation of this or is there one?


I have done the deed. Didst thou not hear a noise?

BBS Signature

Response to A S: Arrays 2005-11-17 14:27:55


At 11/17/05 02:22 PM, glompho wrote: can someone do a more indepth explanation of this or is there one?

nested arrays are arrays INSIDE arrays. you can use them for 3d rendering, or just for storing array information within an array. you can create them with the new Array(); command:
var nest:Array = new Array([0, 1, 0], [1, 0, 0], [1, 1, 1]);

or you can create them with blank array declaration:
var nest:Array = [[0, 1, 0], [1, 0, 0], [1, 1, 1]];

to call a whole array, you just select the index of the outermost array:
nest[1] would return [1, 0, 0]

but to call a value from inside a nested array, you select the index of that array youve just indexed:
nest[1][0] would return 1

hope that helps...

BBS Signature

Response to A S: Arrays 2005-11-17 14:33:29


thanks a lot !


I have done the deed. Didst thou not hear a noise?

BBS Signature

Response to A S: Arrays 2005-11-17 15:24:00


Heh I absolutely <3 arrays in Flash, since they're extremely flexible. Here's a few tricks that I find very useful:

Copy an Array:
myArray.slice(0,-1);

Empty an Array:
while(myArray.length > 0) {
myArray.pop();
}

LIFO Stack:
Only add elements with push(), remove with pop();
(LIFO = Last In First Out)

FIFO Stack:
Only add elements with push(), remove with shift();
(FIFO = First In First Out)

Response to A S: Arrays 2005-11-17 15:27:25


At 11/17/05 03:24 PM, newsdee wrote: Copy an Array:
myArray.slice(0,-1);

oooh, neat trick...

Empty an Array:
while(myArray.length > 0) {
myArray.pop();
}

wouldnt myArray = new Array(); work?


BBS Signature

Response to A S: Arrays 2005-11-17 15:49:48


At 11/17/05 03:24 PM, newsdee wrote: Heh I absolutely <3 arrays in Flash, since they're extremely flexible. Here's a few tricks that I find very useful:

Copy an Array:
myArray.slice(0,-1);

Empty an Array:
while(myArray.length > 0) {
myArray.pop();
}

you can also use concat() with no parameters to clone an array
and like someone said, why use a loop to empty an array when you can just do = new Array()


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature

Response to A S: Arrays 2005-11-18 02:41:54


Using a loop to empty an array is useful when you have objects in it. It avoids memory leaks (crap objects staying in memory). Had the issue with Flash 7, though there may have been improvements to garbage collection in Flash 8.

Response to A S: Arrays 2005-11-18 02:55:49


At 11/18/05 02:41 AM, newsdee wrote: Using a loop to empty an array is useful when you have objects in it. It avoids memory leaks (crap objects staying in memory).

you could just do it by completely deleting the variable first:

delete myArray;
var myArray:Array = new Array();

by the way...

A S: Arrays


BBS Signature

Response to A S: Arrays 2005-11-18 03:21:45


Here is a good example of Arrays put to good use. Here is a quick flashcard program I made in flash, while I Was too damn lazy to make sure that it doesnt choose a random answer that is the real answer, it still gets the job done.

Flash Card

Response to A S: Arrays 2005-11-19 09:09:35


I've bene having difficulty with arrays. For some reason, putting an array variable (e.g. weapons[0]) into a dynamic text box variable box won't show it. If this is true always, is there a way of manipulating the array so I can grab any part of it for use in a dynamic text box.


I'm back! on a temporary basis. No-one can remember who I am! but I don't really mind.

Response to A S: Arrays 2005-11-19 09:12:04


At 11/19/05 09:09 AM, Devenger wrote: I've bene having difficulty with arrays. For some reason, putting an array variable (e.g. weapons[0]) into a dynamic text box variable box won't show it. If this is true always, is there a way of manipulating the array so I can grab any part of it for use in a dynamic text box.

Just give the textbox an instance name (i.e. showArrayVar) then on the frame:

showArrayVar.text = weapons[0];


Sup, bitches :)

BBS Signature

Response to A S: Arrays 2005-11-19 09:14:57


I was sort of trying to avoid having to do that :( It sort of takes away the point of me even having all these arrays storing anything and everything about everything.

Thanks for the help anyway...


I'm back! on a temporary basis. No-one can remember who I am! but I don't really mind.

Response to A S: Arrays 2005-11-19 09:17:03


I've noticed the same problem in the past. I found the simpliest way to work around it was to create a variable of type string that always contained the information for the text box and then changed that variable to match the part of the array you wanted to display. Example:

var names = new Array("BOB", "JOE", "JASON", "MIKE", "BILLY", "JOSH");
var curName = names[2];

And then in the variable box for the dynamic text put 'curName'.

Response to A S: Arrays 2005-11-19 09:21:29


That's the way I've started doing it. Athought it is a bit of a laborious process, because I have various dyn. text boxes, which reference data in a _root array about the names of various weapons, and clicking them brings up other details from the same array.

Still, I better get to it. ^-^


I'm back! on a temporary basis. No-one can remember who I am! but I don't really mind.