flash actionscript:
Ok, some pain here. I believe this is another ActionScript 2.0 bug. At least,
it's going to throw a few people for a loop.
If you initialize a class member like this:
class X {
var myArray = new Array();
}
All instances of X will share THE SAME myArray. So when you manipulate one,
you're manipulating all of them.
One must do this instead:
class X {
var myArray;
function X() { myArray = new Array(); }
}
Example: Put this in your classpath and from your first frame, run:
TestArray.test("Hello");
class TestArray
{ // TestArray
var children:Array = new Array();
function TestArray(prefix)
{ // TestArray
// !! Uncomment the following line to make it work.
// children = new Array();
dumpChildren("Before adding to children:");
for (var i=0; i<2; i++)
children.push(prefix + i);
dumpChildren("After adding children");
} // TestArray
function dumpChildren(msg)
{ // dumpChildren
trace(msg);
for (var i=0; i<children.length; i++)
trace(" child: " + children);
} // dumpChildren
static function test(msg)
{ // test
trace("Testing x: " + msg);
var x = new TestArray("x.");
trace("Testing y: " + msg);
var y = new TestArray("y.");
trace("Done.");
x.dumpChildren("x after constructors are done.");
y.dumpChildren("y after constructors are done.");
} // test
} // TestArray
produces:
Testing x: Hello
Before adding to children:
After adding children
child: x.0
child: x.1
Testing y: Hello
Before adding to children:
child: x.0
child: x.1
After adding children
child: x.0
child: x.1
child: y.0
child: y.1
Done.
x after constructors are done.
child: x.0
child: x.1
child: y.0
child: y.1
y after constructors are done.
child: x.0
child: x.1
child: y.0
child: y.1