Flash / Actionscript: Create an Array of Objects from a Unique Class
When starting in AS2, I found it difficult to make an array of objects after creating my own class. Hopefully, this little tutorial will make it easier for others.
I spent 8 hours trying to figure this out initially. To me actionscript handles this a little differently than other programming languages.
Basically we will create a small class, create an object with that class, and then load that object into an array.
First we create our class. Remember to create a class, you have to do it from a seperate actionscript file.
//in testclass.as
class testClass
{
//Establishes the first variable in your testClass
public var first: String;
}
Now, here is the heavily commented code that should be in your main actionscript file.
//Import your unique class--testClass
import testClass;
//create tC object from your test class
//create an array to hold the objects
var theArray:Array = new Array();
//create loop with i as the counter
for (var i=0; i<4; i++)
{
//create tC object from your test class
var tC:testClass = new testClass();
//assign text to the first variable.
//i included to prove that we are assigning different data
//to each object in the array
tC.first = "asdf" + i; //asdf0, asdf1, etc.
//push the object into the array
theArray.push(tC);
}
//Another loop with ii as counter
for (var ii=0; ii<4; ii++)
{
//shows that the array contains an array of objects
//and demostrates how to extract the values from them
trace (theArray[ii].first);
//asdf0,asdf1, etc.
}
Easy, huh?










Juan Pablo said on October 16, 2008
Fantastic!
Thx!!!!
tim said on February 12, 2009
I tried a few different ways I thought made sense, too, but nothing worked. I didn’t think I’d find much specifics on inserting objects into an array, but this helped a LOT. THANKS!
HrlD said on February 27, 2009
Thanks a lot, this was really useful. The same method transfers to AS3 by the way if anyone was wondering
SelfLearner said on May 25, 2009
Perfect,
it saved my day…
thanks
Balaganesh Damodaran said on September 4, 2009
You don’t really need to create a new class to hold your object. You can simply define your object inline with a variable declaration.
ex:
protected var testClass:Object = {first:null};
The rest of the code remains the same and this works the same as putting the object in a class of its own.
EDIT: Apparently this method overwrites stuff inside the array, so the object definition should be inside the for loop.
barockeuse said on December 8, 2009
One question .. when a class is created in a separate actionscript file, when you build the swf, is the as class file still necessary for the swf to run ?
Thank you for replying … It’s important for me !
^ o ^
Anonymous said on March 7, 2010
thanks. It was great.
but one question, how can I make an array from an object like a movieClip or graphics in flash?
anton said on March 20, 2010
Hello!
I have an array of objects but when I try to pop an object from the array II take the following error:
“ReferenceError: Error #1069: Property pop not found on Tweet and there is no default value.”
I use the pop like this:
var foo = tweetsAr[1].pop();
Tweet is my custom class. any suggestion how to use pop?
anton said on March 20, 2010
oops… my mistake!
pop does remove only the last element of the array.
the proper pop syntax is:
var foo = tweetsAr.pop();
somayeh said on January 26, 2012
Perfect Perfect Perfect
tnx ;)