-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|512|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
SoCoder -> Article Home -> Coding Basics


 
Jayenkai
Created : 17 August 2007
 
System : Nintendo DS
Language : FastBasic

Flash's Arrays and Objects

A quick explaination of the different styles of data storage in Actionscript

Flash's Arrays and Objects. You can think of them like Blitz's Arrays and Types.

For reference, I will be coding this with SoThink SWF Quicker, which is <$100, as opposed to the >$500 that Macromedia's Flash is!
Due to this, minor differences might need to be made when using "Real Flash", so watch out if you're daft enough to have paid that much.

I'm needing to learn about these to build up a new flash game, so I thought I'd post it to the Tutorials to help any future Flash coders.

Simple Arrays
To define an array in Flash, just tell the system that it's an array.

There's no need to define limits. No requirement for variable types. In fact, really, anything goes in Flash! It's very kind, allowing you to do all manner of things.



We can add information either by using numbers, or by pushing, popping, shifting and unshifting data.
Note, however, that popping and shifting data removes that information from the list.



So, it might be useful to push elements into the array, but popping them back out probably isn't the best!

Once we have information in our array, we can use Flash's sorting method to rearrange it.



That's arrays in their simplest form. A nice set of numbered boxes, in order, easily accessible.
Flash, though, has another type of array, derived from Hash Tables.

Hash Table Arrays

Instead of using numbers to move to each element, we use strings. This means that an array can hold lots of different named data, but it also removes the listing ability, so we need to keep track of the strings we use.



Notice that the string 17 still gives us the numbered element 17, and also shifts the length of the array up to 17. Also note, though, that the other strings do not appear in the ordered list.
This can be really handy if you want to store temporary numbers that will be used for the array, but not neccessarily inside it's ordered list. eg, you might want to switch two bits of data inside the array.


A way to switch elements in an array without keeping track of all your temporary variables! (Although I usually just use "Temp" anyway, but whatever!)

So, if we can use Strings inside arrays, we can add extra info into those strings to create arrays inside our arrays.

What!?!?

Arrays of Arrays


We add variables to our string to give us a string variable combo. So long as we continue to use the same combos we can place all sorts of information into our array.

Want a Three Dimensional array in a really easy way?
myArray[x+":"+y+":"+z]

Want a list of highscore boards?
myArray["Board"+Board+":Rank"+Rank]

Want to store all sorts of data for each enemy?
myArray["Enemy"+Enemy+"_XPosition"]
myArray["Enemy"+Enemy+"_YPosition"]
myArray["Enemy"+Enemy+"_Health"]

You can use this type of array for storing all sorts of stuff.
It's handy, but because it uses the Hash Table style of storage, you can't flip through it like you can a standard array. The array has no length, because there are no numerical element names.

How do we flip through lists of data?
Well, first we'll have to see how Objects work, and then we can take those and integrate them into our arrays.

Ready?

Objects

Objects are a bit like Blitz's types, except you only get one of them per definition.
That is, you can define an Object, but not multiple instances of that object.



Just like the arrays, you can add bits and pieces of information into the object as you're going along, but again, you only get one of these to use. So what we really need is a way to hold lots of objects. To do this, we create an array of them.

Arrays of Objects



Fairly simple, but strangely complex!
First we set up a normal everyday array. Into that array we add whole chunks of data.
To add data into an element we use {}, with our different field information pieces inside the squiggly brackets.
To retrieve this information we use the squarebrackets to suggest which element we want to access, and then the .Field style to retrieve each bit of data from that object.

Notice how we can now move through the elements using a simple loop, use the .length method to find the number of elements, and even use the .sortOn method to reorganise the data.

And there you have it.. Arrays of Objects.
Incredibly useful stuff for all kind of games in flash.

 

Comments