I recently stumbled upon something that was driving me nuts. Not sure how I never noticed this before. If you have a class something like this:
class MyClass{private var myList:Array=new Array();
public function MyClass(){}
public function pushItem(item:Object){ myList.push(item);}
public function popItem():Object{ return myList.pop();}}
And then create two instances of that class inside your FLA like’a so:
var a:MyClass=new MyClass();var b:MyClass=new MyClass();
Then, push some items to each instance’s array:
a.pushItem("Hello");b.pushItem("Fred");
In the above example “a” should contain “Hello” and “b” should contain “Fred”.
Now trace the array in instance “a”:
trace(a.popItem()); //returns Fred
Now… This is the part where I started to go crazy. That makes no sense. Thankfully I ran into some details on this over at OSFlash. Visit http://osflash.org/flashcoders/as2 and look for the portion that headlines with: “Why does my initializer get shared across all instances like it’s static?” Turns out that is the way that AS2 compiles down to AS1 code behind the scenes. The fix is pretty simple. You still create the variable reference in the main portion of the class but actually assign it within the constructor of the class like so:
class MyClass{private var myList:Array;
public function MyClass(){ myList = new Array()}
public function pushItem(item:Object){ myList.push(item);}
public function popItem():Object{ return myList.pop();}}
A big thanks to the fellas over at OSFlash for posting that.