ActionScript2’s DON’T!

This is just a small note for all of those who is still developing using AS2.

The complete DON’T for AS2 classes is:

Never ever do create objects of your members arrays in declaration. Create them in the constructor instead.

You see, ActionScript 2 syntax allows the following:

class myAs2Class
  private var myMemberArray:Array = new Array();
  function myAs2Class()

In that case, myMemberArray will be, by some reason, created as a static member. No matter how many instances of myAs2Class you will create, they ALL will operate with a single ‘static-like’ array in spite of the fact you didn’t declare it to be static. I didn’t check if Adobe or Macromedia has an explanation of this, but that’s what I want to share basing on my own experience and hours of debug.

Do the following instead:

class myAs2Class
  private var myMemberArray:Array;
  function myAs2Class()
     myMemberArray = new Array();

This will guarantee that a new array is created each time a new instance of your ActionScript2 class is created.

3 Responses to “ActionScript2’s DON’T!”

  1. 1 Ali

    God point, Paul. Thanks for that.

  2. 2 Shawn

    Thanks for the info. Been trying to debug this for over an hour.

  3. 3 ChR1Zm0

    I spent about 2 hours to figure that out – and then I found this in flash’s documentation:

    “NOTE: When you initialize arrays inline, only one array is created for all instances of the class.”

    …if anyone knows a good reason, WHY adobe/macromedia implemented it that way… Just let me know ๐Ÿ˜‰

Leave a Reply