In AS2 typecasting helps uncover bugs at compile time, so its a good idea to use them. Typecasting also enables class insight through popup menus when you type a period after your variable name.

In AS3 typecasting is a much cooler as it directly affects the amount of memory allocated for the variable. You can read about typecasting in AS2 and AS3 in my blog entry:
<http://www.jamesor.com/2006/08/23/strong-typing-comes-of-age-in-avm2/>

In your class there are a few ways to handle the initialization. You can do it at the field level like this:

class ArrayTest {
  public var aryItems:Array = new Array();
  public function test (str:String):Void
  {
    this.aryItems.push (str);
    trace (this.aryItems);
  }
}

Or you can do it in the class' constructor:

class ArrayTest {
  public var aryItems:Array;
  public function ArrayTest ()
  {
    this.aryItems = new Array();
  }
  public function test (str:String):Void
  {
    this.aryItems.push (str);
    trace (this.aryItems);
  }
}

Or you can create an init() function that
can be used by the constructor as well as being called again after object creation.

class ArrayTest {
  public var aryItems:Array;
  public function ArrayTest ()
  {
    init();
  }
  public function init ():Void
  {
    this.aryItems = new Array();
    // other object initializing stuff
  }
  public function test (str:String):Void
  {
    this.aryItems.push (str);
    trace (this.aryItems);
  }
}

There are more ways to do it than that but those are pretty common and should get you started.

You might want to get used to using typecasting because if you ever move on to AS3 you will have to use it as its a requirement.


James O'Reilly  —  Consultant
Adobe Certified Flash Expert
http://www.jamesor.com
Design • Code • Train



Jon Bennett wrote:
Hi,

got a AS NooB Q.

I'm working on a project and have decided to finally jump ship to AS2,
but I've run into a (probably obvious) problem which has me a bit
stumped.

I've written a class, in which I have an array, but the array always
traces 'undefined' and I can't work out why.

right, so example code:

// ArrayTest.as

class ArrayTest {

    public var items_arr:Array;
public function ArrayTest ()
    {
    }
public function test (str:String)
    {
        this.items_arr.push (str);
        trace (this.items_arr);
    }
}

// Timeline

// import class
import ArrayTest.as;
// create instance
var Test:ArrayTest = new ArrayTest();
// add some values to the array
Test.test ('foo');
Test.test ('bar');

I'm pretty sure this is just me not grasping something bleedin' obvious!

tia,

jon

_______________________________________________
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com

Reply via email to