Helpful Information
 
 
Category: Flash Help
guts of actionscript

i know actionscript is not supposed to be a hardcore programming language. but while recently working on a project which makes heavy use of arrays i noticed some quirks that would have been useful to know before ever starting out...for example:
create an array:
myArray = new Array(1,2,3,4,5);
then use it in another to create a two dimensional array
myNewArray = new Array(myArray);
originally i thought this could work as a copy constructor but no luck...but anyway, if i now alter the array myArray:
myArray[2] = 20;
and trace myNewArray[0] it gives me
1,2,20,4,5
as if myNewArray[0] contains a reference to myArray. Now to go a bit further i set myArray to null, thinking myNewArray[0] will have a null pointer...NOPE! "1,2,20,4,5" is still in myNewArray[0]

I had a class that dealt with this called Organization of Programming Languages...Not sure what it's called at other schools but it deals with how parameters are passed, recursion issues and general info about how a certain language will deal with everything...is there any documentation out there that has looked into actionscript...the help files with flash are good syntax references but not for deeper programming issues...rob

as if myNewArray[0] contains a reference to myArray. Now to go a bit further i set myArray to null, thinking myNewArray[0] will have a null pointer...NOPE! "1,2,20,4,5" is still in myNewArray[0]


what i can tell you from knowing javascript (ecma bla - the same like ActionScript in Flash5)
1. yes, myNewArray[0] contains a reference to myArray. you initialized it like that.
2. if you set myArray to null, you reset the reference of myArray to "the Array" in memory. but it is still linked to myNewArray[0], so garbage collection will not free the memory.

i never used arrays this way, for compatibility issues i still use the "old fashioned way" described in the Flash4 Manual:
myArray_1_1=...
myArray_1_2=...
and the eval command.

Hiya,

You could always implement your own copy contructor for the Array object. Add the following code at the very start of your Flash movie...



Array.prototype.copy = function () {
newArray = new Array();

for (count = 0; count < this.length; count++) {
newArray[count] = this[count];
}

return newArray;
}


This adds a copy() method to the Array object, which you can use to create a duplicate of an array. You'll have to call the copy() method explicitly during assignment, but it's a tidy way of implementing a very useful feature.

Here's some code that you can use to test it out...




// Create array
myArray = new Array(1,2,3,4,5);

// Create a copy of the array
myOtherArray = myArray.copy();

// Change elements in the first array, so we can tell them apart
myArray[0] = 100;

// Output first element of each array
trace("myArray[0]=" add myArray[0]);
trace("myOtherArray[0]=" add myOtherArray[0]);


This will produce the following output...



myArray[0]=100
myOtherArray[0]=1


I hope this helps!

Regards,

Steve Webster
Author - Foundation PHP for Flash (http://www.phpforflash.com)

till yesterday i thought i was quite good at flash...

hey man, thanx this is valuable information to me! OOP in Flash... didnīt think this was possible.


greetings,
M.Hirsch










privacy (GDPR)