You are here: Articles > Programming > Programming

 See more articles about "Programming "

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?

 

Also see ...

Flash/Actionscript Basics: If - Then - Else Statement Syntax
H3Creating a basic if then statement in flash or actionscript is easy. Here is the correct syntax. /H3PThe basic syntax is the following: br / br /span style="font weight: bold"if (condition) br / { br / statement(s); br / } br //span br / br /Here i

Assembly: division
H3How to divide large numbers with assembly language, and where the results end up./H3Pnumbers are most commonly divided by smaller numbers (eg a 16 bit number divided by an 8 bit number) br / br /The numerator must be placed in specific registers and the denomenator is supplied as a singl

Assembly: Bit Shifting
H3Shift bits left or right by a desired numebr of places/H3P br / br / br /Bit shifting is an easy task. br /Shift left (in this case by 8): br /div class="code" br /shl EAX,8 br /P br /or to Shift right (in this case by 8): br /div class="code" br /

Assembly: Bit Rotating
H3Shift bits left or right by a desired numebr of places, with a wrap around to the other side/H3PThis method saves all bits. br /Say you shifted AX by 8, in either direction the new AX value would hold AH as the old AL and AL would become the old AH. This is done with a temporary copy of t

Assembly: Bit Rotating (carry flag)
H3Shift bits left or right by a desired numebr of places, with a wrap around to the other side through the carry flag/H3PThis method saves all bits. br /Say you shifted AX by 8, in either direction the new AX value would hold AH as the old AL and AL would become the old AH. br /This method